# Class 12 IP 065 Ch 1 Python Pandas 1 Type C Long Answer Questions

### Programming Problems (Type-C)

#### 10. Write a program to create three different Series objects from the three rows of a DataFrame df.

Ans:

import pandas as pd

import numpy as np

dic = {‘Maths’:[25,35,69],

‘Science’: [35,45,25],

‘SSt’: [65,25,85]}

dfScore = pd.DataFrame(dic, index=[‘Row-1′,’Row-2’, ‘Row-3’] )

print(“=”*30)

print(“DataFrame is”)

print(“-“*20)

print(dfScore)

serRow1 = dfScore.loc[‘Row-1’]

serRow2 = dfScore.loc[‘Row-2’]

serRow3 = dfScore.loc[‘Row-3’]

print(“=”*30)

print(“Series 1 : Row-1”)

print(“-“*20)

print(serRow1)

print(“=”*30)

print(“Series 2 : Row-2”)

print(“-“*20)

print(serRow2)

print(“=”*30)

print(“Series 3 : Row-3”)

print(“-“*20)

print(serRow3)

print(“=”*30)

#### 11. Write a program to create a Series object from an ndarray that stores characters from â€˜aâ€™ to ‘gâ€™.

Ans:

import pandas as pd

import numpy as np

string = ‘abcdefg’

charArr = np.fromiter(string, dtype=”U2″)

serChar = pd.Series(charArr)

print(“=”*30)

print(“Series are :”)

print(“-“*20)

print(serChar)

print(“=”*30)

#### 12. Write a program to create a Series object that stores the table of number 5.

Ans:

import pandas as pd

import numpy as np

serTable = pd.Series([5*ctr for ctr in range(1,11)])

print(“=”*30)

print(“Series are :”)

print(“-“*20)

print(serTable)

print(“=”*30)

#### 13. Write a program to create a Dataframe that stores two columns, which store the Series objects of the previous two questions (10 and 11).

Ans:

import pandas as pd

import numpy as np

charArr = np.fromiter(‘abcdefg’, dtype=”U2″)

serChar = pd.Series(charArr)

serTable = pd.Series([5*ctr for ctr in range(1,11)])

dfObj = pd.DataFrame({‘alphabets’:serChar, ‘Table of 5’:serTable})

print(“=”*30)

print(“DataFrame is :”)

print(“-“*20)

print(dfObj)

print(“=”*30)

#### 14. Write a program to create a Dataframe storing salesmen details (name, zone, sales) of five salesmen.

Ans:

import pandas as pd

import numpy as np

dicSales = {‘Name’:[‘Amit’, ‘Sumit’, ‘Meena’, ‘Teena’, ‘Sonal’],

‘Zone’: [‘Zone-a’, ‘Zone-b’, ‘Zone-c’, ‘Zone-d’, ‘Zone-e’],

‘Sales’: [2500,6500,7500,8400,3600]}

dfSales = pd.DataFrame(dicSales)

print(“=”*30)

print(“DataFrame is :”)

print(“-“*20)

print(dfSales)

print(“=”*30)

#### 15. Four dictionaries store the details of four employees-of-the-month as (empno, name). Write a program to create a dataframe from these.

Ans:

import pandas as pd

import numpy as np

dic1 = {’empno’:1025, ‘name’: ‘Anjeev’}

dic2 = {’empno’:1045, ‘name’: ‘Naman’}

dic3 = {’empno’:1087, ‘name’: ‘Tarun’}

dic4 = {’empno’:1036, ‘name’: ‘Rashmi’}

dfEmp = pd.DataFrame([dic1, dic2, dic3, dic4])

print(“=”*30)

print(“DataFrame is :”)

print(“-“*20)

print(dfEmp)

print(“=”*30)

#### 16. A list stores three dictionaries each storing details, (old price, new price, change). Write a program to create a dataframe from it.

Ans:

import pandas as pd

import numpy as np

dic1 = {‘old price’:25.00, ‘new price’:28.00, ‘change’:3.00}

dic2 = {‘old price’:44.00, ‘new price’:45.00, ‘change’:1.00}

dic3 = {‘old price’:15.00, ‘new price’:20.00, ‘change’:5.00}

dfPrice = pd.DataFrame([dic1, dic2, dic3])

print(“=”*30)

print(“DataFrame is :”)

print(“-“*20)

print(dfPrice)

print(“=”*30)