Dataframe programs
Dataframe programs
Dataframe programs
Solution :
import pandas as pd
empty_df = pd.DataFrame()
print(empty_df)
Output:
Program 2. Write a python program to create a dataframe named as students using a list of names of 5 students.
Solution:
import pandas as pd
students = ["Ram","Aman","Akash","Ramesh","Virat"]
students = pd.DataFrame(students,columns=["Name"])
print(students)
Output:
Program 3. Write a Python program to create a dataframe players using a list of names and scores of the previous
three matches.
Solution :
import pandas as pd
data = [["Virat",55,66,31],["Rohit",88,66,43],["Samson",99,101,68]]
players = pd.DataFrame(data, columns = ["Name","Match-1","Match-2","Match-3"])
print(players)
Output :
Program 4. Write a Python program to create a dataframe salesman using the series sales_person which stored
saleman names and quantity of sales of August
Solution:
import pandas as pd
sales_person = [["Ram",55],["Ajay",22],["Vijay",67],["Sachin",44]]
salesman = pd.DataFrame(sales_person,columns=["Name","Sales(August)"])
print(salesman)
Output :
Program 5. Write a python program to create a dataframe countries using a dictionary which stored country name,
capitals and populations of the country.
Solution:
import pandas as pd
country_data = {"Country Name":["India","Canada","Australia"], "Capital": ["New Delhi","Ottawa","Canberra"],
"Population" : ["136 Cr","10 Cr","50 Cr"] }
countries = pd.DataFrame(country_data)
print(countries)
Output :
Program 6. Write a python program to Iterate dataframe given as follows by its rows.
Solution:
import pandas as pd
data = [["Virat",55,66,31],["Rohit",88,66,43],["Samson",99,101,68]]
players = pd.DataFrame(data, columns = ["Name","Match-1","Match-2","Match-3"])
for index, row in players.iterrows():
print(index, row.values)
Output :
Program 7. Write a python program to Iterate dataframe Given as follows by its columns.
Solution:
import pandas as pd
sales_person = [["Ram",55],["Ajay",22],["Vijay",67],["Sachin",44]]
salesman = pd.DataFrame(sales_person,columns=["Name","Sales(August)"])
for index, row in salesman.iterrows():
print(index, row["Name"],row["Sales(August)"])
Output :
Program 8. Print scores of previous two matches along with their names using iterrows function. (Use following
dataframe)
Solution:
import pandas as pd
data = [["Virat",55,66,31],["Rohit",88,66,43],["Samson",99,101,68]]
players = pd.DataFrame(data, columns = ["Name","Match-1","Match-2","Match-3"])
for index, row in players.iterrows():
print(index, row["Name"],row["Match-2"],row["Match-3"])
Output :
Program 9. Print sales of salesman along with their index using iteritems(). (Using following dataframe)
Solution :
import pandas as pd
sales_person = [["Ram",55],["Ajay",22],["Vijay",67],["Sachin",44]]
salesman = pd.DataFrame(sales_person,columns=["Name","Sales(August)"])
for index, row in salesman.iterrows():
print(index, row["Name"],row["Sales(August)"])
Output :
Program 10. Consider the following dataframe players and write a python program for given query :
a. Make a total of score from the dataframe players and display their rank according their total scores.
Solution
import pandas as pd
data = [["Virat",55,66,31],["Rohit",88,66,43],["Samson",99,101,68],["Pant",77,55,21]]
players = pd.DataFrame(data, columns = ["Name","Match-1","Match-2","Match-3"])
players['Total_score'] = players['Match-1'] + players['Match-2'] + players["Match-3"]
players['Rank'] = players['Total_score'].rank(ascending=0)
print(players)
Output :
Program 11. Consider following student Dataframe and Write python program for the given query :
Output :
Program 12. Consider the following salesman dataframe, Write python program to execute given problem.
a. Insert two columns at the last of dataframe salesman named city and state using insert function.
Solution :
import pandas as pd
sales_person = [["Ram",55],["Ajay",22],["Vijay",67],["Sachin",44]]
salesman = pd.DataFrame(sales_person,columns=["Name","Sales(August)"])
salesman.insert(2,"City",["Mumbai","Baroda","Udaipur","Banglore"])
salesman.insert(3,"State",["Maharashtra","Gujarat","Rajasthan","Karnataka"])
print(salesman)
Output :
Program 13. Write a python 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.
Solution :
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)
Output
Program 14 : Consider two series objects staff and salaries that store the number of people in various offices
branches and salaries distributed in these branches, respectively.
Write python program to create another Series object that stores average salary per branch and then create a
DataFrame object from these Series object.
Solution :
import pandas as pd
staff = pd.Series([20,36,44])
salaries = pd.Series([279000, 396800, 563000])
avg = salaries/staff #it will create avg salary object
org = { 'people' : staff, 'Amount' : salaries, 'AverageSalary' : avg}
dtf = pd.DataFrame(org)
print(dtf)
output :
Program 15 : Write a python program to create a DataFrame to store weight, age and names of 3 people. Print
DataFrame and its Transpose.
Solution :
import pandas as pd
dtf = pd.DataFrame({
'Weight' : [42, 75, 66],
'Name' : ['Aman', 'Ramesh', 'Ayush'],
'Age' : [ 15,22,35]
})
print('Origrnal DataFrame')
print(dtf)
print('Transpose')
print(dtf.T)
output :
Program 16. In an online contest two 2-player teams points in 4 rounds are stored in two dataframes as shown below
:
Team 1’s points df1 Team 2’s points df2
P1 P2 P1 P2
1 700 490 1 1100 1400
2 975 460 2 1275 1260
3 970 570 3 1270 1500
4 900 590 4 1400 1190
Write the python programs for the following problems based on the above given two dataframes
a. Calculate total points earned by both the teams in each round.
b. Display how much point difference Team2 has with Team1
Solution a. output : a
import pandas as pd
#Team 1 data
d1 = {'p1' : {'1' : 700, '2' : 975, '3' : 970, '4' : 900},
'p2' : {'1' : 490, '2' : 460, '3' : 570, '4' : 590}
}
#Team 2 data
d2 = {'p1' : {'1' : 1100, '2' : 1275, '3' : 1270, '4' : 1400},
'p2' : {'1' : 1400, '2' : 1260, '3' : 1500, '4' : 1190}
}
dtf1 = pd.DataFrame(d1)
dtf2 = pd.DataFrame(d2)
print("Team 1's Performance : ")
print(dtf1)
print("Team 2's Performance : ")
print(dtf2)
print("points earned by both teams : ")
Tpoints = dtf1+dtf2
print(Tpoints)
Solution b.
import pandas as pd Output b.
#Team 1 data
d1 = {'p1' : {'1' : 700, '2' : 975, '3' : 970, '4' : 900},
'p2' : {'1' : 490, '2' : 460, '3' : 570, '4' : 590}
}
#Team 2 data
d2 = {'p1' : {'1' : 1100, '2' : 1275, '3' : 1270, '4' : 1400},
'p2' : {'1' : 1400, '2' : 1260, '3' : 1500, '4' : 1190}
}
dtf1 = pd.DataFrame(d1)
dtf2 = pd.DataFrame(d2)
print("Team 1's Performance : ")
print(dtf1)
print("Team 2's Performance : ")
print(dtf2)
print("points difference Team 2 has with Team 1: ")
print(dtf1.rsub(dtf2))
Output :
a. Write a python program to sort the values of above dataframe named as prodtf in the order of wheat
production.
b. Write a python program to sort the values of above dataframe named as prodtf in the order of Fruits
production in descending order
Solution :
import pandas as pd
import numpy as np
iprod = {
'Rice' : {'AP' : 7452.4, 'GJ' : 1930.0, 'KR' : 2604.8, 'PB':11586.2, 'TR' : 814.6, 'UP' : 2604.8},
'Wheat' : {'AP' : np.NaN, 'GJ' : 2737.0, 'KR' : np.NaN, 'PB': 16440.5, 'TR' : 0.6, 'UP' : 30056.0},
'Pulses' : {'AP' : 931.0, 'GJ' : 818.0, 'KR' : 1.7, 'PB':33.0, 'TR' : 23.2, 'UP' : 8184.4},
'Fruits' : {'AP' : 7830.0, 'GJ' : 11950.0, 'KR' : 114.1, 'PB':7152.0, 'TR' : 44.6, 'UP' : 142604.8},
}
prodtf = pd.DataFrame(iprod) Output
print("original DataFrame : ")
print(prodtf)
print("sorting in order of Wheat : ")
#sorting in ascending order by wheat.....
print(prodtf.sort_values(by = ['Wheat']))
print("sorting in order of Fruits in Descending order : ")
#sorting in descending order by Fruits.....
print(prodtf.sort_values(by = ['Fruits'], ascending = False))
Program 19 : Create a DatFrame Sales where each row contains the item category, item name, and expenditure. Locate
the 3 largest and 3 smallest values of expenditure of this dataframe.
Solution :
import pandas as pd
sales = pd.DataFrame({
'ItemCat' : ['A', 'B', 'A', 'A', 'B', 'C', 'B', 'C'],
'ItemName' : ['ipad', 'LCD', 'iphone', 'iwatch', 'projector', 'Harddisk', 'Smartbord', 'pendrive'],
'Expenditure' : [288000, 356000, 497000, 315000, 413000, 45000, 211000,21000]
})
print('Following is sales dataframe : ')
print(sales)
print('3 Largest expenditure values : ')
print(sales.sort_values(by = ['Expenditure'], ascending = False).head(3))
print('3 Smallest expenditure values : ')
print(sales.sort_values(by = ['Expenditure'], ascending = False).tail(3))
Output :
Program 20 : Plot following data on line chat with following properties :
Program 21 : A Shivalik restaurant has recorded the following data into their register for their income by Drinks and
Food. Plot them on the line chart.
Program 22 : Consider the following data of a medical store and plot the data on the line chart:
Use above data and subplot sanitizer data and handwash data
Solution :
import matplotlib.pyplot as pp
mon =['March','April','May','June','July','August']
san = [4400,4500,5500,6000,5600,6300]
hw = [6500,5000,5800,6300,6200,4500]
pp.subplot(2,1,1)
pp.plot(mon,san,label='Sanitizer',color='b',linestyle='dashed', linewidth=4,\
marker='o', markerfacecolor='k', markeredgecolor='r')
pp.title("Fitwell Medical Store")
pp.legend()
pp.subplot(2,1,2)
pp.plot(mon,hw,label='Handwash',color='r',linestyle='dashed', linewidth=4,\
marker='v', markerfacecolor='k', markeredgecolor='b')
pp.xlabel("Months")
pp.ylabel("Covid Protections")
pp.legend()
pp.show()
Output :
1 6
2 18
3 10
4 5
Solution : Output:
import matplotlib.pyplot as pp
overs =[1,2,3,4]
runs=[6,18,10,5]
pp.bar(overs,runs,color='m')
pp.xlabel('Overs')
pp.xlabel('Runs')
pp.title('Bowling Spell Analysis')
pp.show()
Program 24 : Write a python program to create a horizontal ba chart for India’s medal tally as per following data.
Info Gold Silver Bronze Total
India medal 26 20 20 66
Solution :
import matplotlib.pyplot as plt
info = ['Gold', 'Silver', 'Bronze', 'Total']
India= [26,20,20,66]
plt.ylabel("Medal Type")
plt.xlabel("Medal Count")
plt.title("India's Medal Tally in Commonwealth 2018 :")
x = range(len(info))
plt.barh(x,India, color=['gold', 'silver', 'brown','black'])
plt.show()
Output :
Program 25 : Write a python program to create a pie bar chart for India’s total medal tally of commonwealth games
2018 as per following data
Total = 198, 136, 66, 82, 46, 37, 36, 44, 24, 14
Countries = Australia, England, India, Canada, new Zealand, south Africa, Wales, Scotland, Nigeria, Cyprus
Solution :
import matplotlib.pyplot as plt
total = [198, 136, 66, 82, 46, 37, 36, 44, 24, 14]
contries = ['Australia', 'England', 'India', 'Canada', 'new Zealand', 'south Africa', 'Wales', 'Scotland',
'Nigeria', 'Cyprus']
plt.title("Commonwealth Games 2018 medal Tally")
plt.axis("equal")
expl = [0,0,0.2,0,0,0,0,0,0,0]
plt.pie(total, labels = contries, explode = expl, autopct = "%5.1f%%")
plt.show()
Output :