Page 74 - IPP-12-2024
P. 74
PRACTICAL FILE
CLASS – XII Informatics Practices with Python
Program 1: Write a program to create a pandas series from a dictionary of values and a ndarray.
Solution:
import pandas as pd
dictionary={'A':10,'B':20,'C':30}
series=pd.Series(dictionary)
print(series)
Output:
A 10
B 20
C 30
dtype: int64
Program 2. Write a program to combine number of series to form a dataframe.
Solution:
import numpy as np
ser1 = pd.Series(list('abcedfghijklmnopqrstuvwxyz'))
ser2 = pd.Series(np.arange(26))
df = pd.concat([ser1, ser2], axis=1)
df = pd.DataFrame({'col1': ser1, 'col2': ser2})
print(df.head())
Output:
col1 col2
0 a 0
1 b 1
2 c 2
3 e 3
4 d 4
Program 3: Write a program to create a DataFrame quarterly sales where each row contains the
item category, item name, and expenditure. Group the rows by the category, and print the total
expenditure per category.
Solution:
import pandas as pd
dic={'itemcat':['car','AC','Aircooler','Washing Machine'],
'itemname':['Ford','Hitachi','Symphony','LG'],
'expenditure':[7000000,5000,12000,14000]}
quartsales=pd.DataFrame(dic)
print(quartsales)
qs=quartsales.groupby('itemcat')
print('Result after Filtering DataFrame')
print(qs[['itemcat','expenditure']].sum())
Output:
itemcat itemname expenditure