Raj Series and Dataframe
Raj Series and Dataframe
A) Series program:
1] Write a python code to create a Series object temp temperature of
seven days of week. Its indexes should ‘Sunday’, ‘Monday’…’Saturday’
ANS:
import pandas as pd
import numpy as np
s={'Monday':29,'Tuesday':31,'Wednesday':25,'Thursday':27,'Friday':26,'Saturday':21,'Sunday
':30}
temp=pd.Series(s)
print(temp)
print(temp.mean())
#OR
print(temp.sum()/7)
#display monday,wednesday and friday and sunday temperature.
#This is displaying alternate days temperature.
print(temp['Monday'::2])
output:
2] Three Series object store the marks of 10 students in three terms . Roll number of
students from index of these series object. The three series object have the same indexes.
Calculate the total weighted marks obtained by per students
ANS:
import pandas as pd
import numpy as np
term1=pd.Series([42,45,48],index=[1,2,3])
term2=pd.Series([25,35,38],index=[1,2,3])
term3=pd.Series([45,29,29],index=[1,2,3])
print('Marks of first term:''\n',term1)
print('Marks of second term:''\n',term2)
print('Marks of third term:''\n',term3)
print('Final marks:''\n',term1*25/100+term2*25/100+term3*50/10)
OUTPUT:
3]Write a program to create three different Series object from the three columns of
DataFrame.
ANS:
import pandas as pd
import numpy as np
a={'roll':21,'name':'sudhanshu','fees':20000}
c={'roll':23,'name':'suraj','fees':15000}
b={'roll':25,'name':'raj','fees':18000}
l=[a,b,c]
d=pd.DataFrame(l)
#d is created DataFrame
print(d)
x=pd.Series(d['roll'])
y=pd.Series(d['name'])
z=pd.Series(d['fees'])
print('Series 1:''\n',x)
print('Series 2:''\n',y)
print('Series 3:''\n',z)
ouput:
4] Write a program to create three different Series objects from the three rows of
DataFrame.
ANS:
import pandas as pd
import numpy as np
a={'roll':21,'name':'sudhanshu','fees':20000}
c={'roll':23,'name':'suraj','fees':15000}
b={'roll':25,'name':'raj','fees':18000}
l=[a,b,c]
d=pd.DataFrame(l)
print(d)
x=pd.Series(d.iloc[0,:])
y=pd.Series(d.iloc[1,:])
z=pd.Series(d.iloc[2,:])
print(x)
print(y)
print(z)
output:
5]Write a program to create a Series object from ndarray that stores character from ‘a’ to
‘g’.
ANS:
import pandas as pd
import numpy as np
a=np.array(['a','b','c','d','e','f','g'])
print(a)
s=pd.Series(a)
print(s)
output:
6] Write a program to create a Series object that stores the table of number 5.
ANS:
import pandas as pd
a=range(5,51,5)
b=pd.Series(a)
print(b)
output:
7]Write a program to create a DataFrame that stores two columns , which store the series
object of the previous two question( 4 and 5).
ANS:
import pandas as pd
import numpy as np
a=np.array(list('abcdefghij'))
print(a)
s1=pd.Series(a)
b=np.arange(5,55,5)
s2=pd.Series(b)
d=pd.DataFrame({1:a,2:b})
print(d)
output:
8] Write code to print all the information about a Series object .
ANS:
import pandas as pd
import numpy as np
a=np.arange(10,20,2)
s=pd.Series([5,1,5,None,4],index=np.arange(0,10,2))
print(s)
print('Indexes of Series:''\n',s.index)
print('Values/Data in Series:''\n',s.values)
print('Type of data in Series:',s.dtype)
print('Shape of the Series:',s.shape)
print('Reserved bytes for Series:',s.nbytes)
print('Number of dimension of Series:',s.ndim)
print('Number of values in Series:',s.size)
print('Does Series have any NAN‘s:',s.hasnans)
print('Is the Series empty:',s.empty)
output:
9] Write a program to create a DataFrame storing salesman details (name,zone,sales) of
five salesman.
ANS:
import pandas as pd
a={'name':'suraj','sale':15000,'zone':'solapur'}
b={'name':'raj','sale':25000,'zone':'pune'}
c={'name':'sudhanshu','sale':20000,'zone':'satara'}
d={'name':'swarup','sale':100,'zone':'sangola'}
e={'name':'pushkaraj','sale':96000,'zone':'mumbai'}
D={'I':a,'II':b,'III':c,'IV':d,'V':e}
d=pd.DataFrame(D)
print(d)
output:
10]Four dictonaries store the details of four employes-of-the-month as (empo,name).
Write a program to create a DataFrame of these.
ANS:
import pandas as pd
a={'empno':1,'name':'Janhavi'}
b={'empno':2,'name':'Sanyogita'}
c={'empno':3,'name':'Sachi'}
d={'empno':4,'name':'vaishnavi'}
l=[a,b,c,d]
D=pd.DataFrame(l)
print(D)
output:
11] A list of 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
a={'old price':500,'new price':550,'change':50}
b={'old price':720,'new price':790,'change':70}
c={'old price':60,'new price':95,'change':35}
d=[a,b,c]
D=pd.DataFrame(d)
print(D)
output:
12] Create a dataframe to store 2 team details as Cricket Score Board and perform
Operations on Dataframe.
ANS:
import pandas as pd
a=pd.Series([2.5,4,5,1.5])
b=pd.Series([3,4.5,2.5,5.5])
d=pd.DataFrame({'CSK':a,'MI':b})
d.index=['IPL','20-20','Test Series','One day']
print(d)
#Adding new team
print("Adding new column")
d.loc[:,'RCB']=[2,2.3,3.5,1.5]
print(d)
#adding new row/match
print("adding new row")
d.loc['5 days series',:]=[8,6,9]
print(d)
#displaying few rows
print("displaying few rows")
print(d.iloc[1:3])
#displaying few columns
print("displaying few columns")
print(d.loc[:,'MI':'RCB'])
#or
print(d[['MI','RCB']])
#renaming rows and columns
print("renaming rows and columns")
d=d.rename(index={'IPL':1,'20-20':2,
'Test Series':3,'One day':4,'5 days series':5},
columns={'RCB':'GT'},inplace=False)
print(d)
#modify column values
print("modify column values")
d.loc[:,'MI']=d.loc[:,'MI']+2
print(d)
#deleting row/column
print("del first row")
d=d.drop([1])
print(d)
print("del GT column")
d=d.drop(['GT'],axis=1)
print(d)
#OR
del d['GT']
print(d)
output:
13] Store Item details into CSV and read from CSV.
ANS:
#storing item into CSV file
import pandas as pd
d=pd.DataFrame([[21,25,44],[48,36,15],[45,25,12],[36,25,16]])
d.to_csv("one_csv.csv")
print(d)
#Reading from CSV file:11
import pandas as pd
x=pd.read_csv("one_csv.csv")
print('one_csv.csv:')
print(x)
output:
14] CSV arguments nrows and header
ANS:
import pandas as pd
x=pd.read_csv("one_csv.csv",nrows=2,header=1)
#nrows prints only first 2 rows
#header makes the data in row ’1’ as column
print(x)
#Simple histogram
plt.title("simple histogram")
plt.hist(x,bins,color="magenta")
plt.show()
#Horizontal histogram
plt.title("Horizontal histogram")
plt.hist(x,bins,orientation='horizontal',color='cyan')
plt.show()
#Step histogram:
plt.title("step histogram")
plt.hist(x,bins,histtype="step",color="orchid")
plt.show()
output:
===The End===