12 Ip
12 Ip
Example 2D_Dictionary_ValuesAsLists : Given a dictionary that stores the students name, mark and sprt value
dict1={
'Students' : ['Ruchika','Neha','Mark','Gurjyot','Jamaal'],
'Marks' : [79.5, 83.75, 74, 88.5, 89],
'Sport' : ['Cricket', 'Badminton', 'Football', 'Athletics', 'Kabaddi']
}
Write code to create and display the dataframe using above dictionary.
import pandas as pd
dict1={
'Students' : ['Ruchika','Neha','Mark','Gurjyot','Jamaal'],
'Marks' : [79.5, 83.75, 74, 88.5, 89],
'Sport' : ['Cricket', 'Badminton', 'Football', 'Athletics', 'Kabaddi']
}
df1=pd.DataFrame(dict1)
print(df1)
Example 22: Create and display a DataFrame from a 2D dictionary, Sales, which stores the quarter-wise sales
as inner dictionary for two years, as shown below:
Sales={‘yr1’ : {‘Qtr1’: 34500, ‘Qtr2’: 56000, ‘Qtr3’: 47000, ‘Qtr4’: 49000},
‘yr2’ : {‘Qtr1’: 44900, ‘Qtr2’: 46100, ‘Qtr3’: 57000, ‘Qtr4’: 59000}}
import pandas as pd
Sales={
'yr1' : {'Qtr1': 34500, 'Qtr2': 56000, 'Qtr3': 47000, 'Qtr4': 49000},
'yr2' : {'Qtr1': 44900, 'Qtr2': 46100, 'Qtr3': 57000, 'Qtr4': 59000}
}
dfsales=pd.DataFrame(Sales)
print(dfsales)
Example 2D_Dictionary_ValuesAsDictionary : Create and display a DataFrame from a 2D dictionary as shown
below:
people={‘Sales’ : {‘name’: ‘Rohit’, ‘age’: ‘24’, ‘Gender’: ‘Male’},
‘Marketing’ : {‘name’: ‘Neha’, ‘age’: ’25’, ‘Gender’: ‘Female’}}
import pandas as pd
people={
'Sales' : {'name': 'Rohit', 'age': '24', 'Gender': 'Male'},
'Marketing' : {'name': 'Neha', 'age': '25', 'Gender': 'Female'}
}
dfpeople=pd.DataFrame(people)
print(dfpeople)
(i) The index labels of df3 will include : A, B, Q3, Q4, Qtr1, Qtr2, Qtr4.
The total number of indexes is equal to total unique inner keys, i.e., 7.
(i) The column names of df3 will be : 1, 2
Example 24: Write a program to create a dataframe from a list containing dictionaries of the sales
performance of four zonal offices. Zone names should be the row labels.
import pandas as pd
zoneA = {'Target' : 56000, 'Sales' : 58000}
zoneB = {'Target' : 70000, 'Sales' : 68000}
zoneC = {'Target' : 75000, 'Sales' : 78000}
zoneD = {'Target' : 60000, 'Sales' : 61000}
sales = [zoneA, zoneB, zoneC, zoneD]
saleDf = pd.DataFrame(sales, index=['zoneA','zoneB','zoneC','zoneD'])
print(saleDf)
Example 25: Write a program to create a dataframe from a 2D list. Specify own index labels.
import pandas as pd
list2 = [ [25, 45, 60], [34, 67, 89], [88, 90, 56] ]
df2 = pd.DataFrame(list2, index=[‘row1’, ‘row2’, ‘row3’])
print(df2)
Example 26: Write a program to create a dataframe from a list containing 2 lists, each containing Target and
actual Sales figures of four zonal offices. Give appropriate row labels.
import pandas as pd
Target = [56000, 70000, 75000, 60000]
Sales = [58000, 68000, 78000, 61000]
ZoneSales = [Target, Sales]
zsaleDf=pd.DataFrame(zoneSales,
columns=[‘ZoneA’, ‘ZoneB’, ‘ZoneC’, ‘ZoneD’],
index=[‘Target’,’Sales’])
print(zsaleDf)
import pandas as pd
import numpy as np
arr1=np.array([ [11,12], [13,14], [15,16] ], np.int32)
dtf2=pd.DataFrame(arr1)
print(dtf2)
Example 28: Write a program to create a DataFrame from a 2D array as shown below:
101 113 124
130 140 200
115 216 217
import pandas as pd
import numpy as np
arr2=np.array([ [101,113,124], [130,140,200], [115,216,217] ])
dtf3=pd.DataFrame(arr2)
print(dtf3)
4. Creating a DataFrame Object from a 2-D dictionary with Values as Series Objects
Example 29: Consider two series objects staff and salaries that store the number of people in various office
branches and salaries distributed in these branches, respectively.
Write a aprogram to create another Series object that sotres average salary per branch an then create
DataFrame object frome these Series objects.
import pandas as pd
import numpy as np
staff=pd.Series([20, 36, 44])
salaries=pd.Series([279000, 396800, 563000])
avg=salaries / staff
org={'people':staff,'Amount':salaries,'Average':avg}
dtf5=pd.DataFrame(org)
print(dtf5)