0% found this document useful (0 votes)
37 views25 pages

Raj Series and Dataframe

The document discusses various programs to create and manipulate pandas Series and DataFrame objects. It includes 21 questions with sample code to create Series from various data, perform operations on Series and DataFrames like indexing, slicing, aggregation, plotting etc. The key focus is on introducing the basics of pandas Series and DataFrames through practical examples and exercises.

Uploaded by

sakibtamboli245
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views25 pages

Raj Series and Dataframe

The document discusses various programs to create and manipulate pandas Series and DataFrame objects. It includes 21 questions with sample code to create Series from various data, perform operations on Series and DataFrames like indexing, slicing, aggregation, plotting etc. The key focus is on introducing the basics of pandas Series and DataFrames through practical examples and exercises.

Uploaded by

sakibtamboli245
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Project on 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)

15] plot a simple line chart.


ANS:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
term1=pd.Series([42,45,48,25,50],index=[1,2,3,4,5])
term2=pd.Series([25,35,38,12,49],index=[1,2,3,4,5])
term3=pd.Series([45,29,29,45,26],index=[1,2,3,4,5])
d={'Term1':term1,'Term2':term2,'Term3':term3}
#making DataFrame to plot linechart
D=pd.DataFrame(d)
print(D)
d['name']=['suraj','raj','sudhanshu','vicky','sakib']
y=d['name']
x=d['Term1']
plt.plot(y,x,label='Term1')
plt.xlabel('Names')
plt.ylabel('Marks')
plt.show()
output:
16] Plot Multiple lines chart.
ANS:
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
a=[52,340,890]
b=[64,480,560]
c=[78,688,1102]
d=[94,766,889]
l=[a,b,c,d]
D=pd.DataFrame(l,columns=[1990,2000,2010])
print(D)
#Required data frame
x=[1990,2000]
y=[D[1990],D[2000]]
plt.plot(x,y)
plt.show()
output:

17] Plot simple bar graph


ANS:
import pandas as pd
import matplotlib.pyplot as plt
x=['jan','feb','march','april','may','june']
y=[17,55,35,22,19,20]
plt.bar(x,y,color=['red','orchid','cyan','orange','pink','purple'])
plt.show()
output:

18] Plot multiple bar graphs on same chart area


ANS:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
term1=pd.Series([42,45,48,25,50],index=[1,2,3,4,5])
term2=pd.Series([25,35,38,12,49],index=[1,2,3,4,5])
term3=pd.Series([45,29,29,45,26],index=[1,2,3,4,5])
d={'Term1':term1,'Term2':term2,'Term3':term3}
D=pd.DataFrame(d)
#Required DataFrame for plotting graph
print(d)
d['name']=['suraj','raj','sudhanshu','vicky','sakib']
y=d['name']
x=d['Term1']
z=d['Term2']
w=d['Term3']
j=np.arange(5)
plt.bar(j+00.0,x,label="Term1",width=0.25)
plt.bar(j+0.25,z,color='r',label='Term2',width=0.25)
plt.bar(j+0.50,w,color='y',label='Term3',width=0.25)
plt.show()
output:
19] Plot horizontal bar graph
ANS:
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
a=[52,340,890]
b=[64,480,560]
c=[78,688,1102]
d=[94,766,889]
l=[a,b,c,d]
D=pd.DataFrame(l,columns=[1990,2000,2010])
#Required data frame.
x=D.columns
y=D.loc[:,:]
plt.barh(x,y)
plt.show()
output:
20] Plot simple histogram
ANS:
import matplotlib.pyplot as plt
import numpy as np
a=[1,45,25,13,45,25,16,14,2,4,16,23,29,18,25,27,36,9,8,27,9,34,42,6,34,31,26,29,35,37,39,1
6,18,16,12,5,9,7,9,45,41,49,46]
blinks=np.arange(0,51,5)
#blinks=[0,10,20,30,40,50]
plt.hist(a,blinks,color='r')
plt.show()
output:
21] Plot a histogram with cumulative, orientation and histtype arguments.
ANS:
import pandas as pd
import matplotlib.pyplot as plt
x=[78,72,69,81,63,65,75,79,74,71,83,71,79,80,69]
bins=[60,65,70,75,80,85]

#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===

You might also like