Page 89 - IPP-12-2024
P. 89
10 307
11 188
12 233
Name: Marks, dtype: int64
Program 14. Write a program to create dataframe for 3 students including name and roll numbers
and add new columns for 5 subjects and 1 column to calculate percentage. It should include random
numbers in marks of all subjects.
Solution:
import pandas as pd
import random
D = {'Roll': [1, 2, 3], 'Name': ['Sangeeta', 'Shanti', 'Swati']}
P = []
C = []
M = []
E = []
H = []
SD = pd.DataFrame(D)
for i in range(3):
P.append(random.randint(1, 101))
C.append(random.randint(1, 101))
M.append(random.randint(1, 101))
E.append(random.randint(1, 101))
H.append(random.randint(1, 101))
SD['Phy'] = P
SD['Chem'] = C
SD['Maths'] = M
SD['Eng'] = E
SD['Hin'] = H
SD['Total'] = SD['Phy'] + SD['Chem'] + SD['Maths'] + SD['Eng'] +
SD['Hin']
SD['Per'] = SD['Total'] / 5
print(SD)
Output:
Roll Name Phy Chem Maths Eng Hin Total
Per
0 1 Sangeeta 22 82 14 91 70 279
55.8
1 2 Shanti 48 32 88 17 58 243
48.6
2 3 Swati 59 59 41 19 10 188
37.6
Program 15. Write a program to import and export data between pandas and CSV file.
Solution:
import pandas as pd
sample_data = {
'name': ['Sachin', 'Vinod', 'Rajesh'],
'SirName': ['Bhardwaj', 'Verma', 'Mishra']
}
df = pd.DataFrame(sample_data)