2. What is Pivoting?
Name any two functions of Pandas which support
pivoting.
3 a) Write a python code to create a dataframe with appropriate headings
from the list given below : ['S101', 'Amy', 70], ['S102', 'Bandhi', 69],
['S104', 'Cathy', 75], ['S105', 'Gundaho', 82]
b)
Write a small python code to create a dataframe with headings(a and b)
from the list given below : [[1,2],[3,4],[5,6],[7,8]]
4. Find the output of the following code:
import pandas as pd
data = [{'a': 10, 'b': 20},{'a': 6, 'b': 32, 'c': 22}]
#with two column indices, values same as dictionary keys
df1 = pd.DataFrame(data, index=['first', 'second'], columns=['a', 'b'])
#With two column indices with one index with other name
df2 = pd.DataFrame(data, index=['first', 'second'], columns=['a', 'b1'])
print(df1) print(df2)
5. Write the code in pandas to create the following dataframes :
df1 df2
mark1 mark2 mark1 mark2
0 10 150 30 20
1 40 45 20 25
2 15 30 20 30
3 40 70 50 30
Write the commands to do the following operations on the dataframes
given above :
(i) To add dataframes df1 and df2.
(ii) To subtract df2 from df1
(iii) To rename column mark1 as marks1in both the dataframes df1 and
df2.
(iv) To change index label of df1 from 0 to zero and from 1 to one.
6. Write Python code to create a Series object Temp1
that stores temperatures of seven days in it. Take any
random seven temperatures.
7. Write code to display the temperatures recorded on:
(i) First 3 days
(ii) Last 3 days.
(iii) Print the average temperature per week
8. Write code to print all the information about a Series
object.
9. Write a program to create a Series object that stores
the table of number 5.
10. Write a program to create a Dataframe that stores
two columns, which store the Series objects of the
previous two questions (8 and 9).
ANSWERS
1.ii) Reindex
2. Pivoting - To reshape or summarise a DataFrame organised by index
or column name
i) pivot( )
ii) pivot_table( )
3.import pandas as pd
d=[[‘S101’,’Amy’,70],[‘S102’,’Bandhi’,69],[‘S104’,’Cathy’,75]],[‘S105’,’G
undaho’,82]]
df=pd.DataFrame(d,columns=[‘ID’,’Name’,’Marks’])
print(df)
b)import pandas as pd
df = pd.DataFrame([[1, 2], [3, 4]], columns = ['a','b'])
df2 = pd.DataFrame([[5, 6], [7, 8]], columns = ['a','b'])
df = df.append(df2)
4. a b
first 10 20
second 6 32
a b1
first 10 NaN
second 6 NaN
5.import pandas as pd
import numpy as np
df1 = pd.DataFrame({'mark1':[30,40,15,40], 'mark2':[20,45,30,70]})
df2 = pd.DataFrame({'mark1':[10,20,20,50], 'mark2':[15,25,30,30]})
print(df1)
print(df2)
(i) print(df1.add(df2))
(ii) print(df1.subtract(df2))
(iii) df1.rename(columns={'mark1':'marks1'}, inplace=True)
print(df1)
(iv) df1.rename(index = {0: "zero", 1:"one"}, inplace = True)
print(df1)
6.import pandas as pd
t=[31,24,30,26,27,26,28]
temp1=pd.Series(t,index=[‘Monday’,’Tuesday’,’Wednesday’,’Thursday’
,’Friday’,’Saturday’,’Sunday’])
print(temp1)
7.print(temp1.head(3))
ii) print(temp1.tail(3))
iii) print(temp1.mean())
8.import pandas as pd
s=pd.Series([22,13,44])
print(s.describe)
9.import pandas as pd
import numpy as np
d=np.arange(5,51,5)
s=pd.Series(d)
print(s)
10. import pandas as pd
import numpy as np
s=pd.Series([22,13,44])
print(s)
d=np.arange(5,51,5)
s1=pd.Series(d)
print(s1)
df=pd.DataFrame({“col”:s,”col2”:s1})
print(df)