Adding source code files for Chapter 6

This commit is contained in:
muassif
2021-03-13 19:25:45 +04:00
committed by GitHub
parent e662762d09
commit 9589675a50
45 changed files with 625 additions and 0 deletions
+15
View File
@@ -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)
+13
View File
@@ -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)
+24
View File
@@ -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])
+17
View File
@@ -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)
+27
View File
@@ -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)
+17
View File
@@ -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))
+20
View File
@@ -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)
+20
View File
@@ -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)