Docsity

Dataframe programmes, Schemes and Mind Maps of Computer science

Toggle sidebar
bg1
Program 1. Write a python program to create an empty dataframe named as empty_df.
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"] }
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Dataframe programmes and more Schemes and Mind Maps Computer science in PDF only on Docsity!

Program 1. Write a python program to create an empty dataframe named as empty_df.

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"])

students = students.append(temp,ignore_index=True) print("----------- Updated DataFrame -------------") print(students) 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 df P 1 P 2 1 700 490 2 975 460 3 970 570 4 900 590 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 Team 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}, Team 2’s points df 2 P 1 P 2 1 1100 1400 2 1275 1260 3 1270 1500 4 1400 1190

print(mskdtf.min(axis = 1, skipna = True)) print("Maximum marks scored in each subject across all the rows : ") print(mskdtf.max(axis = 1, skipna = True)) print("Mode, Median and average for each section :") print("Mode : ") print(mskdtf.mode()) print("Median :") print(mskdtf.median()) print("Average : ") print(mskdtf.mean()) Output : Program 18 : Consider the following DataFrame 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 :

Solution : import matplotlib.pyplot as pp day =['Monday','Tuesday','Wednesday','Thursday','Friday'] dr = [450,560,400,605,580] fd = [490,600,425,610,625] pp.plot(day,dr,label='Drinks',color='g',linestyle='dotted',marker='+') pp.plot(day,fd,label='Food',color='m',linestyle='dashdot',marker='x') pp.title("The Weekly Restaurant Orders") pp.xlabel("Days") pp.ylabel("Orders") pp.legend() pp.show() Output :

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 :

Program 23 : Display following bowling figures through bar chart:

Over’s Runs

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()