diff --git a/Chapter6/comprehension/dictcomp1.py b/Chapter6/comprehension/dictcomp1.py new file mode 100644 index 0000000..bd26e2d --- /dev/null +++ b/Chapter6/comprehension/dictcomp1.py @@ -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) \ No newline at end of file diff --git a/Chapter6/comprehension/gencomp1.py b/Chapter6/comprehension/gencomp1.py new file mode 100644 index 0000000..188bc2a --- /dev/null +++ b/Chapter6/comprehension/gencomp1.py @@ -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) diff --git a/Chapter6/comprehension/list1.py b/Chapter6/comprehension/list1.py new file mode 100644 index 0000000..d9daa11 --- /dev/null +++ b/Chapter6/comprehension/list1.py @@ -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) \ No newline at end of file diff --git a/Chapter6/comprehension/list2.py b/Chapter6/comprehension/list2.py new file mode 100644 index 0000000..a35832a --- /dev/null +++ b/Chapter6/comprehension/list2.py @@ -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) + diff --git a/Chapter6/comprehension/setcomp1.py b/Chapter6/comprehension/setcomp1.py new file mode 100644 index 0000000..d3b8a71 --- /dev/null +++ b/Chapter6/comprehension/setcomp1.py @@ -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) \ No newline at end of file diff --git a/Chapter6/decorator/decorator0-extra.py b/Chapter6/decorator/decorator0-extra.py new file mode 100644 index 0000000..cb25eab --- /dev/null +++ b/Chapter6/decorator/decorator0-extra.py @@ -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() \ No newline at end of file diff --git a/Chapter6/decorator/decorator1.py b/Chapter6/decorator/decorator1.py new file mode 100644 index 0000000..c508e64 --- /dev/null +++ b/Chapter6/decorator/decorator1.py @@ -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() \ No newline at end of file diff --git a/Chapter6/decorator/decorator2.py b/Chapter6/decorator/decorator2.py new file mode 100644 index 0000000..011a16a --- /dev/null +++ b/Chapter6/decorator/decorator2.py @@ -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) \ No newline at end of file diff --git a/Chapter6/decorator/decorator3.py b/Chapter6/decorator/decorator3.py new file mode 100644 index 0000000..98c37a2 --- /dev/null +++ b/Chapter6/decorator/decorator3.py @@ -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)) \ No newline at end of file diff --git a/Chapter6/decorator/decorator4.py b/Chapter6/decorator/decorator4.py new file mode 100644 index 0000000..3aeb021 --- /dev/null +++ b/Chapter6/decorator/decorator4.py @@ -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)) \ No newline at end of file diff --git a/Chapter6/decorator/decorator5.py b/Chapter6/decorator/decorator5.py new file mode 100644 index 0000000..3654651 --- /dev/null +++ b/Chapter6/decorator/decorator5.py @@ -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") \ No newline at end of file diff --git a/Chapter6/decorator/decorator6-extra.py b/Chapter6/decorator/decorator6-extra.py new file mode 100644 index 0000000..43d35a5 --- /dev/null +++ b/Chapter6/decorator/decorator6-extra.py @@ -0,0 +1,20 @@ +from functools import wraps + +def makebold(fn): + @wraps(fn) + def wrapped(*args, **kwargs): + return "" + fn(*args, **kwargs) + "" + return wrapped + +def makeitalic(fn): + @wraps(fn) + def wrapped(*args, **kwargs): + return "" + fn(*args, **kwargs) + "" + return wrapped + +@makebold +@makeitalic +def say(): + return 'Hello' + +print(say()) \ No newline at end of file diff --git a/Chapter6/decorator/log.txt b/Chapter6/decorator/log.txt new file mode 100644 index 0000000..d427bee --- /dev/null +++ b/Chapter6/decorator/log.txt @@ -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 diff --git a/Chapter6/dictionary/dictionary1.py b/Chapter6/dictionary/dictionary1.py new file mode 100644 index 0000000..44d4358 --- /dev/null +++ b/Chapter6/dictionary/dictionary1.py @@ -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)) \ No newline at end of file diff --git a/Chapter6/dictionary/dictionary2.py b/Chapter6/dictionary/dictionary2.py new file mode 100644 index 0000000..a12f2fb --- /dev/null +++ b/Chapter6/dictionary/dictionary2.py @@ -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)) diff --git a/Chapter6/dictionary/dictionary3.py b/Chapter6/dictionary/dictionary3.py new file mode 100644 index 0000000..835b159 --- /dev/null +++ b/Chapter6/dictionary/dictionary3.py @@ -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']) diff --git a/Chapter6/dictionary/dictionary4.py b/Chapter6/dictionary/dictionary4.py new file mode 100644 index 0000000..c990271 --- /dev/null +++ b/Chapter6/dictionary/dictionary4.py @@ -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) \ No newline at end of file diff --git a/Chapter6/innerfunction/inner1.py b/Chapter6/innerfunction/inner1.py new file mode 100644 index 0000000..0104586 --- /dev/null +++ b/Chapter6/innerfunction/inner1.py @@ -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() diff --git a/Chapter6/innerfunction/inner2.py b/Chapter6/innerfunction/inner2.py new file mode 100644 index 0000000..93540a6 --- /dev/null +++ b/Chapter6/innerfunction/inner2.py @@ -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)) + diff --git a/Chapter6/lambda/lambda1.py b/Chapter6/lambda/lambda1.py new file mode 100644 index 0000000..4ed0ef1 --- /dev/null +++ b/Chapter6/lambda/lambda1.py @@ -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) diff --git a/Chapter6/lambda/lambda2.py b/Chapter6/lambda/lambda2.py new file mode 100644 index 0000000..7c86160 --- /dev/null +++ b/Chapter6/lambda/lambda2.py @@ -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) diff --git a/Chapter6/lambda/lambda3.py b/Chapter6/lambda/lambda3.py new file mode 100644 index 0000000..7c25728 --- /dev/null +++ b/Chapter6/lambda/lambda3.py @@ -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) diff --git a/Chapter6/mapfilterreduce/filter1.py b/Chapter6/mapfilterreduce/filter1.py new file mode 100644 index 0000000..506812a --- /dev/null +++ b/Chapter6/mapfilterreduce/filter1.py @@ -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) diff --git a/Chapter6/mapfilterreduce/filter2.py b/Chapter6/mapfilterreduce/filter2.py new file mode 100644 index 0000000..be8ec65 --- /dev/null +++ b/Chapter6/mapfilterreduce/filter2.py @@ -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) diff --git a/Chapter6/mapfilterreduce/filter3.py b/Chapter6/mapfilterreduce/filter3.py new file mode 100644 index 0000000..a30aa8d --- /dev/null +++ b/Chapter6/mapfilterreduce/filter3.py @@ -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) diff --git a/Chapter6/mapfilterreduce/map1.py b/Chapter6/mapfilterreduce/map1.py new file mode 100644 index 0000000..90f6518 --- /dev/null +++ b/Chapter6/mapfilterreduce/map1.py @@ -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) \ No newline at end of file diff --git a/Chapter6/mapfilterreduce/map2.py b/Chapter6/mapfilterreduce/map2.py new file mode 100644 index 0000000..feef913 --- /dev/null +++ b/Chapter6/mapfilterreduce/map2.py @@ -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) diff --git a/Chapter6/mapfilterreduce/map3.py b/Chapter6/mapfilterreduce/map3.py new file mode 100644 index 0000000..e3d5819 --- /dev/null +++ b/Chapter6/mapfilterreduce/map3.py @@ -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) diff --git a/Chapter6/mapfilterreduce/reduce1.py b/Chapter6/mapfilterreduce/reduce1.py new file mode 100644 index 0000000..4d4feb3 --- /dev/null +++ b/Chapter6/mapfilterreduce/reduce1.py @@ -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) diff --git a/Chapter6/mypandas/advance/pandastrick1.py b/Chapter6/mypandas/advance/pandastrick1.py new file mode 100644 index 0000000..da5310c --- /dev/null +++ b/Chapter6/mypandas/advance/pandastrick1.py @@ -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) \ No newline at end of file diff --git a/Chapter6/mypandas/advance/pandastrick2.py b/Chapter6/mypandas/advance/pandastrick2.py new file mode 100644 index 0000000..e6a05d1 --- /dev/null +++ b/Chapter6/mypandas/advance/pandastrick2.py @@ -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) \ No newline at end of file diff --git a/Chapter6/mypandas/advance/pandastrick3.py b/Chapter6/mypandas/advance/pandastrick3.py new file mode 100644 index 0000000..b8a1679 --- /dev/null +++ b/Chapter6/mypandas/advance/pandastrick3.py @@ -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)]) \ No newline at end of file diff --git a/Chapter6/mypandas/advance/pandastrick4.py b/Chapter6/mypandas/advance/pandastrick4.py new file mode 100644 index 0000000..b4bffb7 --- /dev/null +++ b/Chapter6/mypandas/advance/pandastrick4.py @@ -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')]) \ No newline at end of file diff --git a/Chapter6/mypandas/advance/pandastrick5.py b/Chapter6/mypandas/advance/pandastrick5.py new file mode 100644 index 0000000..8b01624 --- /dev/null +++ b/Chapter6/mypandas/advance/pandastrick5.py @@ -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))) \ No newline at end of file diff --git a/Chapter6/mypandas/extra1.py b/Chapter6/mypandas/extra1.py new file mode 100644 index 0000000..9d16166 --- /dev/null +++ b/Chapter6/mypandas/extra1.py @@ -0,0 +1,6 @@ +#between function + +import pandas as pd + +df = pd.read_csv("weekly_weather.csv") +print(df) \ No newline at end of file diff --git a/Chapter6/mypandas/extra2.py b/Chapter6/mypandas/extra2.py new file mode 100644 index 0000000..594fd95 --- /dev/null +++ b/Chapter6/mypandas/extra2.py @@ -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:])) \ No newline at end of file diff --git a/Chapter6/mypandas/operations/pandas1.py b/Chapter6/mypandas/operations/pandas1.py new file mode 100644 index 0000000..e5a453d --- /dev/null +++ b/Chapter6/mypandas/operations/pandas1.py @@ -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) \ No newline at end of file diff --git a/Chapter6/mypandas/operations/pandas2.py b/Chapter6/mypandas/operations/pandas2.py new file mode 100644 index 0000000..88c40a0 --- /dev/null +++ b/Chapter6/mypandas/operations/pandas2.py @@ -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) diff --git a/Chapter6/mypandas/operations/pandas3.py b/Chapter6/mypandas/operations/pandas3.py new file mode 100644 index 0000000..90bcc9b --- /dev/null +++ b/Chapter6/mypandas/operations/pandas3.py @@ -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]) + diff --git a/Chapter6/mypandas/operations/pandas4.py b/Chapter6/mypandas/operations/pandas4.py new file mode 100644 index 0000000..b8cee47 --- /dev/null +++ b/Chapter6/mypandas/operations/pandas4.py @@ -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) + diff --git a/Chapter6/mypandas/operations/pandas5.py b/Chapter6/mypandas/operations/pandas5.py new file mode 100644 index 0000000..7297889 --- /dev/null +++ b/Chapter6/mypandas/operations/pandas5.py @@ -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) + + + diff --git a/Chapter6/mypandas/operations/pandas6.py b/Chapter6/mypandas/operations/pandas6.py new file mode 100644 index 0000000..1963e29 --- /dev/null +++ b/Chapter6/mypandas/operations/pandas6.py @@ -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)) + + + diff --git a/Chapter6/mypandas/operations/pandas7.py b/Chapter6/mypandas/operations/pandas7.py new file mode 100644 index 0000000..dc21bd7 --- /dev/null +++ b/Chapter6/mypandas/operations/pandas7.py @@ -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) + + + diff --git a/Chapter6/mypandas/operations/pandas8.py b/Chapter6/mypandas/operations/pandas8.py new file mode 100644 index 0000000..b83a7be --- /dev/null +++ b/Chapter6/mypandas/operations/pandas8.py @@ -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) + + + diff --git a/Chapter6/mypandas/weekly_weather.csv b/Chapter6/mypandas/weekly_weather.csv new file mode 100644 index 0000000..0a3a037 --- /dev/null +++ b/Chapter6/mypandas/weekly_weather.csv @@ -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 \ No newline at end of file