Adding source code files for Chapter 6
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
#dictcomp1.py
|
||||
|
||||
dict1 = {'a': 100, 'b': 200, 'c': 300}
|
||||
dict2 = {x : int(y/2) for (x, y) in dict1.items() if y <=200}
|
||||
print(dict2)
|
||||
|
||||
dict3 = {}
|
||||
for x,y in dict1.items():
|
||||
if y <= 200:
|
||||
dict3[x] = int(y/2)
|
||||
print(dict3)
|
||||
@@ -0,0 +1,5 @@
|
||||
#gencomp1.py
|
||||
|
||||
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
gen1 = (x for x in list1 if x % 2 ==0)
|
||||
print(gen1)
|
||||
@@ -0,0 +1,10 @@
|
||||
#list1.py
|
||||
|
||||
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
list2 = [x+1 for x in list1]
|
||||
print(list2)
|
||||
|
||||
list3 = []
|
||||
for x in list1:
|
||||
list3.append(x+1)
|
||||
print(list3)
|
||||
@@ -0,0 +1,6 @@
|
||||
#list2.py
|
||||
|
||||
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
list2 = [x for x in list1 if x % 2 == 0]
|
||||
print(list2)
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
#setcomp1.py
|
||||
|
||||
|
||||
list1 = [1, 2, 6, 4, 5, 6, 7, 8, 9, 10, 8]
|
||||
set1 = {x for x in list1 if x % 2 ==0}
|
||||
print(set1)
|
||||
|
||||
set2 = set()
|
||||
for x in list1:
|
||||
if x % 2 == 0:
|
||||
set2.add(x)
|
||||
print(set2)
|
||||
@@ -0,0 +1,13 @@
|
||||
# decorator1.py
|
||||
|
||||
def add_hello(myfunc):
|
||||
def _add_hello():
|
||||
print("inside add hello")
|
||||
myfunc()
|
||||
return _add_hello
|
||||
|
||||
@add_hello
|
||||
def hello_world():
|
||||
print("hello world")
|
||||
|
||||
hello_world()
|
||||
@@ -0,0 +1,18 @@
|
||||
# decorator1.py
|
||||
from datetime import datetime
|
||||
|
||||
def add_timestamps(myfunc):
|
||||
def _add_timestamps():
|
||||
print(datetime.now())
|
||||
myfunc()
|
||||
print(datetime.now())
|
||||
return _add_timestamps
|
||||
|
||||
@add_timestamps
|
||||
def hello_world():
|
||||
print("hello world")
|
||||
|
||||
hello_world()
|
||||
|
||||
#hello = add_timestamps(hello_world)
|
||||
#hello()
|
||||
@@ -0,0 +1,19 @@
|
||||
# decorator2.py
|
||||
from datetime import datetime
|
||||
from functools import wraps
|
||||
|
||||
def add_timestamps(myfunc):
|
||||
@wraps(myfunc)
|
||||
def _add_timestamps():
|
||||
print(datetime.now())
|
||||
myfunc()
|
||||
print(datetime.now())
|
||||
return _add_timestamps
|
||||
|
||||
@add_timestamps
|
||||
def hello_world():
|
||||
print("hello world")
|
||||
|
||||
hello_world()
|
||||
help(hello_world)
|
||||
print(hello_world)
|
||||
@@ -0,0 +1,16 @@
|
||||
# decorator3.py
|
||||
from functools import wraps
|
||||
|
||||
def power(func):
|
||||
@wraps(func)
|
||||
def inner_calc(*args, **kwargs):
|
||||
print("Decorating power")
|
||||
n = func(*args, **kwargs)
|
||||
return n
|
||||
return inner_calc
|
||||
|
||||
@power
|
||||
def power_base2(n):
|
||||
return 2**n
|
||||
|
||||
print(power_base2(3))
|
||||
@@ -0,0 +1,18 @@
|
||||
# decorator4.py
|
||||
from functools import wraps
|
||||
|
||||
def power_calc(base):
|
||||
def inner_decorator(func):
|
||||
@wraps(func)
|
||||
def inner_calc(*args, **kwargs):
|
||||
exponent = func(*args, **kwargs)
|
||||
return base**exponent
|
||||
return inner_calc
|
||||
return inner_decorator
|
||||
|
||||
@power_calc(base=3)
|
||||
def power_n(n):
|
||||
return n
|
||||
|
||||
print(power_n(2))
|
||||
print(power_n(4))
|
||||
@@ -0,0 +1,47 @@
|
||||
# decorator5.py
|
||||
from datetime import datetime
|
||||
from functools import wraps
|
||||
|
||||
def add_timestamp(func):
|
||||
@wraps(func)
|
||||
def inner_func(*args, **kwargs):
|
||||
res = "{}: {}\n".format(datetime.now(), func(*args, **kwargs))
|
||||
return res
|
||||
return inner_func
|
||||
|
||||
def file(func):
|
||||
@wraps(func)
|
||||
def inner_func(*args, **kwargs):
|
||||
res = func(*args, **kwargs)
|
||||
with open("log.txt", 'a') as file:
|
||||
file.write(res)
|
||||
return res
|
||||
return inner_func
|
||||
|
||||
def console(func):
|
||||
@wraps(func)
|
||||
def inner_func(*args, **kwargs):
|
||||
res = func(*args, **kwargs)
|
||||
print(res)
|
||||
return res
|
||||
return inner_func
|
||||
|
||||
@file
|
||||
@add_timestamp
|
||||
def log(msg):
|
||||
return msg
|
||||
|
||||
@file
|
||||
@console
|
||||
@add_timestamp
|
||||
def log1(msg):
|
||||
return msg
|
||||
|
||||
@console
|
||||
@add_timestamp
|
||||
def log2(msg):
|
||||
return msg
|
||||
|
||||
log("This is a test message for file only")
|
||||
log1("This is a test message for both file and console")
|
||||
log2("This message is for console only")
|
||||
@@ -0,0 +1,20 @@
|
||||
from functools import wraps
|
||||
|
||||
def makebold(fn):
|
||||
@wraps(fn)
|
||||
def wrapped(*args, **kwargs):
|
||||
return "<b>" + fn(*args, **kwargs) + "</b>"
|
||||
return wrapped
|
||||
|
||||
def makeitalic(fn):
|
||||
@wraps(fn)
|
||||
def wrapped(*args, **kwargs):
|
||||
return "<i>" + fn(*args, **kwargs) + "</i>"
|
||||
return wrapped
|
||||
|
||||
@makebold
|
||||
@makeitalic
|
||||
def say():
|
||||
return 'Hello'
|
||||
|
||||
print(say())
|
||||
@@ -0,0 +1,3 @@
|
||||
2021-03-13 12:53:00.545882: This is a test message for both file and console
|
||||
2021-03-13 12:53:21.075098: This is a test message for file only
|
||||
2021-03-13 12:53:21.075517: This is a test message for both file and console
|
||||
@@ -0,0 +1,8 @@
|
||||
# dictionary1.py
|
||||
|
||||
dict1 = {100:{'name':'John', 'age':24},
|
||||
101:{'name':'Mike', 'age':22},
|
||||
102:{'name':'Jim', 'age':21} }
|
||||
|
||||
print(dict1)
|
||||
print(dict1.get(100))
|
||||
@@ -0,0 +1,21 @@
|
||||
# dictionary2.py
|
||||
#defining inner dictionary 1
|
||||
student100 = {'name': 'John', 'age': 24}
|
||||
|
||||
#defining inner dictionary 2
|
||||
student101 = {}
|
||||
student101['name'] = 'Mike'
|
||||
student101['age'] = '22'
|
||||
|
||||
#assiging inner dictionaries 1 and 2 to a root dictionary
|
||||
dict1 = {}
|
||||
dict1[100] = student100
|
||||
dict1[101] = student101
|
||||
|
||||
#creating inner dictionary directly inside a root dictionary
|
||||
dict1[102] = {}
|
||||
dict1[102]['name'] = 'Jim'
|
||||
dict1[102]['age'] = '21'
|
||||
|
||||
print(dict1)
|
||||
print(dict1.get(102))
|
||||
@@ -0,0 +1,10 @@
|
||||
# dictionary3.py
|
||||
|
||||
dict1 = {100:{'name':'John', 'age':24},
|
||||
101:{'name':'Mike', 'age':22},
|
||||
102:{'name':'Jim', 'age':21} }
|
||||
|
||||
print(dict1.get(100))
|
||||
print(dict1.get(100).get('name'))
|
||||
print(dict1[101])
|
||||
print(dict1[101]['age'])
|
||||
@@ -0,0 +1,10 @@
|
||||
# dictionary4.py
|
||||
|
||||
dict1 = {100:{'name':'John', 'age':24},
|
||||
101:{'name':'Mike', 'age':22},
|
||||
102:{'name':'Jim', 'age':21} }
|
||||
|
||||
del (dict1[101]['age'])
|
||||
print(dict1)
|
||||
dict1[102].pop('age')
|
||||
print(dict1)
|
||||
@@ -0,0 +1,9 @@
|
||||
#inner1.py
|
||||
|
||||
def outer_hello():
|
||||
print ("Hello from outer function")
|
||||
def inner_hello():
|
||||
print("Hello from inner function")
|
||||
inner_hello()
|
||||
|
||||
outer_hello()
|
||||
@@ -0,0 +1,14 @@
|
||||
#inner2.py
|
||||
|
||||
def power_gen_factory(base):
|
||||
def power_calc(exponent):
|
||||
return base**exponent
|
||||
return power_calc
|
||||
|
||||
power_calc_2 = power_gen_factory(2)
|
||||
power_calc_3 = power_gen_factory(3)
|
||||
print(power_calc_2(2))
|
||||
print(power_calc_2(3))
|
||||
print(power_calc_3(2))
|
||||
print(power_calc_3(4))
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
# lambda1.py to get square of each item in a list
|
||||
|
||||
mylist = [1, 2, 3, 4, 5]
|
||||
new_list = list(map(lambda x: x*x, mylist))
|
||||
print(new_list)
|
||||
@@ -0,0 +1,5 @@
|
||||
# lambda2.py to get even numbers from a list
|
||||
|
||||
mylist = [1, 2, 3, 4, 5,6,7,8,9]
|
||||
new_list = list(filter(lambda x: x % 2 == 0, mylist))
|
||||
print(new_list)
|
||||
@@ -0,0 +1,6 @@
|
||||
# lambda3.py to get product of corresponding item in the two lists
|
||||
|
||||
mylist1 = [1, 2, 3, 4, 5]
|
||||
mylist2 = [6, 7, 8, 9]
|
||||
new_list = list(map(lambda x,y: x*y, mylist1, mylist2))
|
||||
print(new_list)
|
||||
@@ -0,0 +1,8 @@
|
||||
# filter1.py to get even numbers from a list
|
||||
|
||||
def is_even(num):
|
||||
return (num % 2 == 0)
|
||||
|
||||
mylist = [1, 2, 3, 4, 5,6,7,8,9]
|
||||
new_list = list(filter(is_even, mylist))
|
||||
print(new_list)
|
||||
@@ -0,0 +1,8 @@
|
||||
# filter1.py to get even numbers from a list
|
||||
|
||||
def is_dublicate(item):
|
||||
return not(item in mylist)
|
||||
|
||||
mylist = ["Orange","Apple", "Banana", "Peach", "Banana"]
|
||||
new_list = list(filter(is_dublicate, mylist))
|
||||
print(new_list)
|
||||
@@ -0,0 +1,14 @@
|
||||
# filter1.py to get even numbers from a list
|
||||
|
||||
def is_even(num):
|
||||
return (num % 2 == 0)
|
||||
|
||||
def contains_e(name):
|
||||
return 'e' in name
|
||||
|
||||
mylist1 = [1, 2, 3, 4, 5,6,7,8,9]
|
||||
mylist2 = ["Orange","Apple", "Banana", "Peach", "Banana"]
|
||||
new_list1 = list(filter(is_even, mylist1))
|
||||
new_list2 = list(filter(contains_e, mylist2))
|
||||
print(new_list1)
|
||||
print(new_list2)
|
||||
@@ -0,0 +1,10 @@
|
||||
#map1.py to get sqauare of each item in a list
|
||||
|
||||
mylist = [1, 2, 3, 4, 5]
|
||||
new_list = []
|
||||
|
||||
for item in mylist:
|
||||
square = item*item
|
||||
new_list.append(square)
|
||||
|
||||
print(new_list)
|
||||
@@ -0,0 +1,8 @@
|
||||
# map2.py to get square of each item in a list
|
||||
|
||||
def square(num):
|
||||
return num * num
|
||||
|
||||
mylist = [1, 2, 3, 4, 5]
|
||||
new_list = list(map(square, mylist))
|
||||
print(new_list)
|
||||
@@ -0,0 +1,9 @@
|
||||
# map3.py to get product of corresponding item in the two lists
|
||||
|
||||
def product(num1, num2):
|
||||
return num1 * num2
|
||||
|
||||
mylist1 = [1, 2, 3, 4, 5]
|
||||
mylist2 = [6, 7, 8, 9]
|
||||
new_list = list(map(product, mylist1, mylist2))
|
||||
print(new_list)
|
||||
@@ -0,0 +1,9 @@
|
||||
# reduce1.py to get sum of numbers from a list
|
||||
from functools import reduce
|
||||
|
||||
def seq_sum(num1, num2):
|
||||
return num1+num2
|
||||
|
||||
mylist = [1, 2, 3, 4, 5]
|
||||
result = reduce(seq_sum, mylist, 10)
|
||||
print(result)
|
||||
@@ -0,0 +1,33 @@
|
||||
# pandastrick1.py
|
||||
import pandas as pd
|
||||
|
||||
weekly_data = {'day':['Monday','Tuesday', 'Wednesday', 'Thursday',
|
||||
'Friday', 'Saturday', 'Sunday'],
|
||||
'temp':[40, 33, 42, 31, 41, 40, 30],
|
||||
'condition':['Sunny','Cloudy','Sunny','Rainy','Sunny',
|
||||
'Cloudy','Rainy']
|
||||
}
|
||||
df = pd.DataFrame(weekly_data)
|
||||
|
||||
#Replacing a numeric value of 40 with 39 across DF
|
||||
df.replace(40,39, inplace=True)
|
||||
|
||||
#Replacing string Sunny with Sun across DF
|
||||
df.replace("Sunny","Sun",inplace=True)
|
||||
|
||||
#Replacing strings starting with Cl with Cloud across DF
|
||||
df.replace(to_replace="^Cl.*",value="Cloud", inplace=True,regex=True)
|
||||
|
||||
#Replacing Day names using a list across DF
|
||||
df.replace(["Monday","Tuesday"],["monday","tuesday"], inplace=True)
|
||||
|
||||
#Replacing Day names using a single dict across DF
|
||||
df.replace({"Wednesday":"wednesday","Thursday":"thursday"}, inplace=True)
|
||||
|
||||
#Replacing Day names using dict for column and value
|
||||
df.replace({"day":"Friday"}, {"day":"friday"}, inplace=True)
|
||||
|
||||
#Replacing name of two days using dicts for column day
|
||||
df.replace({"day":{"Saturday":"saturday", "Sunday":"sunday"},
|
||||
"condition":{"Rainy":"Rain"}}, inplace=True)
|
||||
print(df)
|
||||
@@ -0,0 +1,17 @@
|
||||
# pandastrick2.py
|
||||
import pandas as pd
|
||||
|
||||
weekly_data = {'day':['Monday','Tuesday', 'Wednesday', 'Thursday',
|
||||
'Friday', 'Saturday', 'Sunday'],
|
||||
'temp':[40, 33, 42, 31, 41, 40, 30],
|
||||
'condition':['Sunny,','_Cloudy ','Sunny','Rainy',
|
||||
'--Sunny.','Cloudy.','Rainy']
|
||||
}
|
||||
df = pd.DataFrame(weekly_data)
|
||||
print(df)
|
||||
|
||||
df["condition"] = df["condition"].map(
|
||||
lambda x: x.lstrip('_- ').rstrip(',. '))
|
||||
|
||||
df["temp_F"] = df["temp"].apply(lambda x: 9/5*x+32 )
|
||||
print(df)
|
||||
@@ -0,0 +1,13 @@
|
||||
# pandastrick3.py
|
||||
import pandas as pd
|
||||
|
||||
weekly_data = {'day':['Monday','Tuesday', 'Wednesday', 'Thursday',
|
||||
'Friday', 'Saturday', 'Sunday'],
|
||||
'temp':[40, 33, 42, 31, 41, 40, 30],
|
||||
'condition':['Sunny','Cloudy','Sunny','Rainy','Sunny',
|
||||
'Cloudy','Rainy']
|
||||
}
|
||||
df = pd.DataFrame(weekly_data)
|
||||
|
||||
print(df[(df.temp >= 30) & (df.temp<=40)])
|
||||
print(df[df.temp.between(30,40)])
|
||||
@@ -0,0 +1,13 @@
|
||||
# pandastrick4.py
|
||||
import pandas as pd
|
||||
|
||||
weekly_data = {'day':['Monday','Tuesday', 'Wednesday', 'Thursday',
|
||||
'Friday', 'Saturday', 'Sunday'],
|
||||
'temp':[40, 33, 42, 31, 41, 40, 30],
|
||||
'condition':['Sunny','Cloudy','Sunny','Rainy','Sunny',
|
||||
'Cloudy','Rainy']
|
||||
}
|
||||
df = pd.DataFrame(weekly_data)
|
||||
|
||||
print(df[(df.condition == 'Rainy') | (df.condition == 'Sunny')])
|
||||
print(df[df['condition'].str.contains('Rainy|Sunny')])
|
||||
@@ -0,0 +1,17 @@
|
||||
# pandastrick5.py
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
pd.set_option('display.max_columns', None)
|
||||
|
||||
weekly_data = {'day':['Monday','Tuesday', 'Wednesday', 'Thursday',
|
||||
'Friday', 'Saturday', 'Sunday'],
|
||||
'temp':[40, 33, 42, 31, 41, 40, 30],
|
||||
'condition':['Sunny','Cloudy','Sunny','Rainy','Sunny',
|
||||
'Cloudy','Rainy']
|
||||
}
|
||||
df = pd.DataFrame(weekly_data)
|
||||
|
||||
print(df.describe())
|
||||
print(df.describe(include="all"))
|
||||
print(df.describe(percentiles=np.arange(0, 1, 0.1)))
|
||||
print(df.groupby('condition').describe(percentiles=np.arange(0, 1, 0.1)))
|
||||
@@ -0,0 +1,6 @@
|
||||
#between function
|
||||
|
||||
import pandas as pd
|
||||
|
||||
df = pd.read_csv("weekly_weather.csv")
|
||||
print(df)
|
||||
@@ -0,0 +1,13 @@
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
|
||||
df = pd.DataFrame(np.array([[1, 2, 3], [4, 5, 6]]))
|
||||
print(df)
|
||||
|
||||
data = np.array([['', 'Col1', 'Col2'],
|
||||
['Row1', 1, 2],
|
||||
['Row2', 3, 4]])
|
||||
|
||||
print(pd.DataFrame(data=data[1:, 1:],
|
||||
index=data[1:, 0],
|
||||
columns=data[0, 1:]))
|
||||
@@ -0,0 +1,15 @@
|
||||
# pandas1.py
|
||||
import pandas as pd
|
||||
|
||||
weekly_data = {'day':['Monday','Tuesday', 'Wednesday', 'Thursday',
|
||||
'Friday', 'Saturday', 'Sunday'],
|
||||
'temp':[40, 33, 42, 31, 41, 40, 30],
|
||||
'condition':['Sunny','Cloudy','Sunny','Rain','Sunny',
|
||||
'Cloudy','Rain']
|
||||
}
|
||||
|
||||
df = pd.DataFrame(weekly_data)
|
||||
print(df)
|
||||
|
||||
df1 = df.set_index('day')
|
||||
print(df1)
|
||||
@@ -0,0 +1,13 @@
|
||||
# pandas2.py
|
||||
import pandas as pd
|
||||
|
||||
weekly_data = {'day':['Monday','Tuesday', 'Wednesday', 'Thursday',
|
||||
'Friday', 'Saturday', 'Sunday'],
|
||||
'temp':[40, 33, 42, 31, 41, 40, 30],
|
||||
'condition':['Sunny','Cloudy','Sunny','Rain','Sunny',
|
||||
'Cloudy','Rain']
|
||||
}
|
||||
|
||||
df = pd.DataFrame(weekly_data)
|
||||
df.index = ['MON', 'TUE','WED','THU','FRI','SAT','SUN']
|
||||
print(df)
|
||||
@@ -0,0 +1,24 @@
|
||||
# pandas3.py
|
||||
import pandas as pd
|
||||
|
||||
weekly_data = {'day':['Monday','Tuesday', 'Wednesday', 'Thursday',
|
||||
'Friday', 'Saturday', 'Sunday'],
|
||||
'temp':[40, 33, 42, 31, 41, 40, 30],
|
||||
'condition':['Sunny','Cloudy','Sunny','Rain','Sunny',
|
||||
'Cloudy','Rain']
|
||||
}
|
||||
|
||||
df = pd.DataFrame(weekly_data)
|
||||
df.index = ['MON', 'TUE','WED','THU','FRI','SAT','SUN']
|
||||
#Provide row with label TUE
|
||||
print(df.loc['TUE'])
|
||||
#Provide two rows with label TUE and WED
|
||||
print(df.loc[['TUE','WED']])
|
||||
#provide a temp value from row with label FRI
|
||||
print(df.loc['FRI','temp'])
|
||||
#Provide a row with index 2
|
||||
print(df.iloc[2])
|
||||
#provide a value from a location with
|
||||
# row index 2 and column index 2
|
||||
print(df.iloc[2,2])
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
# pandas4.py
|
||||
import pandas as pd
|
||||
|
||||
weekly_data = {'day':['Monday','Tuesday', 'Wednesday', 'Thursday',
|
||||
'Friday', 'Saturday', 'Sunday'],
|
||||
'temp':[40, 33, 42, 31, 41, 40, 30],
|
||||
'condition':['Sunny','Cloudy','Sunny','Rain','Sunny',
|
||||
'Cloudy','Rain']
|
||||
}
|
||||
|
||||
df = pd.DataFrame(weekly_data)
|
||||
df.index = ['MON', 'TUE','WED','THU','FRI','SAT','SUN']
|
||||
df.loc['TST1'] = ['Test day 1', 50, 'NA']
|
||||
df.loc[7] = ['Test day 2', 40, 'NA']
|
||||
|
||||
print(df)
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
# pandas5.py
|
||||
import pandas as pd
|
||||
|
||||
weekly_data = {'day':['Monday','Tuesday', 'Wednesday', 'Thursday',
|
||||
'Friday', 'Saturday', 'Sunday'],
|
||||
'temp':[40, 33, 42, 31, 41, 40, 30],
|
||||
'condition':['Sunny','Cloudy','Sunny','Rain','Sunny',
|
||||
'Cloudy','Rain']
|
||||
}
|
||||
|
||||
df = pd.DataFrame(weekly_data)
|
||||
df.index = ['MON', 'TUE','WED','THU','FRI','SAT','SUN']
|
||||
|
||||
#Adding a new column and then updating it
|
||||
df['Humidity1'] = [60, 70, 65,62,56,25,'']
|
||||
df['Humidity1'] = [60, 70, 65,62,56,251,'']
|
||||
|
||||
#Inserting a colun at colum index of 2
|
||||
df.insert(2, "Humidity2",[60, 70, 65,62,56,25,''])
|
||||
#df.insert(2, "Humidity2",[60, 70, 65,62,56,25,''])
|
||||
|
||||
#Adding two columns
|
||||
df1 = df.assign(Humidity3 = [60, 70, 65,62,56,25,''],Humidity4 = [60, 70, 65,62,56,25,''])
|
||||
print(df1)
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
# pandas6.py
|
||||
import pandas as pd
|
||||
|
||||
weekly_data = {'day':['Monday','Tuesday', 'Wednesday', 'Thursday',
|
||||
'Friday', 'Saturday', 'Sunday'],
|
||||
'temp':[40, 33, 42, 31, 41, 40, 30],
|
||||
'condition':['Sunny','Cloudy','Sunny','Rain','Sunny',
|
||||
'Cloudy','Rain']
|
||||
}
|
||||
|
||||
df = pd.DataFrame(weekly_data)
|
||||
df.index = ['MON', 'TUE','WED','THU','FRI','SAT','SAT']
|
||||
print(df)
|
||||
print(df.reset_index(drop=True))
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
# pandas7.py
|
||||
import pandas as pd
|
||||
|
||||
weekly_data = {'day':['Monday','Tuesday', 'Wednesday', 'Thursday',
|
||||
'Friday', 'Saturday', 'Sunday'],
|
||||
'temp':[40, 33, 42, 31, 41, 40, 30],
|
||||
'condition':['Sunny','Cloudy','Sunny','Rain','Sunny',
|
||||
'Cloudy','Rain']
|
||||
}
|
||||
|
||||
df = pd.DataFrame(weekly_data)
|
||||
df.index = ['MON', 'TUE','WED','THU','FRI','SAT','SUN']
|
||||
print(df)
|
||||
df1=df.drop(index=['SUN','SAT'])
|
||||
df2=df1.drop(columns=['condition'])
|
||||
|
||||
print(df2)
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
# pandas8.py
|
||||
import pandas as pd
|
||||
|
||||
weekly_data = {'day':['Monday','Tuesday', 'Wednesday', 'Thursday',
|
||||
'Friday', 'Saturday', 'Sunday'],
|
||||
'temp':[40, 33, 42, 31, 41, 40, 30],
|
||||
'condition':['Sunny','Cloudy','Sunny','Rain','Sunny',
|
||||
'Cloudy','Rain']
|
||||
}
|
||||
|
||||
df = pd.DataFrame(weekly_data)
|
||||
df.index = ['MON', 'TUE','WED','THU','FRI','SAT','SUN']
|
||||
print(df)
|
||||
df1=df.rename(index={'SUN': 'SU', 'SAT': 'SA'})
|
||||
df2=df1.rename(columns={'condition':'cond'})
|
||||
|
||||
print(df2)
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
day,temperature,condition
|
||||
Monday,40,Sunny
|
||||
Tuesday,38,Sunny
|
||||
Wednesday,42,Sunny
|
||||
Thursday,37,Rain
|
||||
Friday,35,Cloudy
|
||||
Saturday,40,Cloudy
|
||||
Sunday,30,Rain
|
||||
|
Reference in New Issue
Block a user