0% found this document useful (0 votes)
7 views22 pages

Pandas DataFrame and Series Operations

Uploaded by

13232hrishabh
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)
7 views22 pages

Pandas DataFrame and Series Operations

Uploaded by

13232hrishabh
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

1. To generate a series using a mathematical expression (Multiplication).

import pandas as pd
import numpy as np
s1= [Link] (10,15)
print (s1)
sobj = [Link](index=s1,data=s1*4)
print (sobj)

OUTPUT-
2. To perform indexing, slicing and accessing data from a series

import pandas as pd
s = [Link] ([1,2,3,4,5,6],index = [ 'a','b','c','d','e']
print (s[0])
print (s[ :3])
print (s[ -3: ])
print (s[ : ])
print (s[1 : 4])
print (s[ :: -1])
print (s[ 0::2])

OUTPUT-
3. Illustrate head ( ) and tail ( ) functions on a series.

import pandas as pd
s1 = [Link] ([10,20,30,40,50],index= ['a','b','c','d','e'])
print (s1)
print ([Link](2))
print ([Link] (-2))
print ([Link] (2))
print ([Link] (-2))

output-

[Link] create dataframe from two series of student data.


import pandas as pd

student_marks= [Link]
({"vijay":80,"nikita":92,"rahul":80,"divya":97,"bhavya":95})

student_age= [Link]
({"vijay":18,"nikita":17,"rahul":29,"divya":18,"bhavya":16})

student_df= [Link] ({"marks":student_marks,"age":student_age})

print (student_df)

output-
[Link] display records from the first to the third row.

import pandas as pd

student = {"name":['rinku','rashi','aman','sanam','pinku','pankaj','chinku'],
"English": [67,78,90,79,80,57,78], "economics" :[90,70,89,80,59,80,82],
"IP" : [72,92,90,87,95,87,87]}

df = [Link] (student)

print (df)

print("after slicing")

print (df [1:4])

output-
6. Implementation of iloc and loc attribute with respect to “student”
dataframe

import pandas as pd

student={"rollno":[1,2,3,4,5],"name":
["rinku","deep","shaurya","radhika","rohit"],"english":
[89,90,79,68,56],"economics": [89,69,80,57,56],"IP":[58,90,80,89,94]}

df=[Link] (student)

print (df)

print ([Link][1:3])

print (df .iloc [0:3,0:3])

print (df. iloc [0:3])

output –
7. To sort the data of student dataframe based on marks.

import pandas as pd

stud_marks=[Link]({"vijay":40,"ajay":90,"priam":45,"natik":69,"dee

pak":48})

stud_age=[Link]({"vijay":14,"ajay":19,"priam":15,"natik":19,"deepa

k":18})

stud_df=[Link]({"marks":stud_marks,"age":stud_age})

print(stud_df.sort_values(by=['marks']))

output-
8. Illustrate head ( ) and tail ( ) methods in an employee dataframe.

import pandas as pd

Student = { “Roll No”: [1,2,3,4,5] , “Name”: [‘Rinku’, ‘Deep’, ‘Shaurya’,


‘Radhika’, ‘Rohit’] , “English”: [89,78,89,90,79] , “Economics” :
[ 87,80,60,84,77] , “I.P”: [ 89,78,67,90,92]}

df = [Link] (Student)

print (df)

print ([Link][0:3])

print ([Link][0:3,0:3])

print ([Link] [1:3])

output-
[Link] create a dataframe from more than one Numpy ndarray.

import pandas as pd
import numpy as np
array = [Link]([ [67, 78, 75, 78], [67, 78, 75, 88], [78, 67,
89, 90], [78, 88, 98, 90]])
column_values = ['English', 'Economics', 'IP', 'Accounts']
df = [Link](data=array, columns=column_values)
print(df)
output-

English Economics IP Accounts

0 67 78 75 78

1 67 78 75 88

2 78 67 89 90

3 78 88 98 90

10. To create and open “[Link]” file using pandas.

import pandas as pd

df=pd.read_csv("D:\\anushka\\[Link]")

print(df)

output-
[Link] create a student CSV file from data frame.

import pandas as pd

student={"rollno":[1,2,3,4,5],"stud_name":
["nikita","niketan","aashiv","ravi","jiya"],"marks":[92,67,48,92,59],"class":
["11y","12g","9b","4d","4f"]}

df=[Link](student,columns=['rollno','stud_name','marks','class'])

df.to_csv("D:\\anushka\\[Link]")

output-
[Link] modify the employee name from mahima to harsh in [Link]

import pandas as pd
df=pd.read_csv("D:\\anushka\\[Link]")
print("dataframe contents without updation")
print(df)
print()
[Link][3,'name']="harsh"
df.to_csv("D:\\anushka\\[Link]",index=False)
print("dataframe contents after updation")
print(df)
output-

[Link] add legends, titles and labels to a line plot with multiple lines.

import [Link] as plt


x=[1,2,3]
y=[5,7,3]
[Link](x,y,label='first line ')
x2= [ 1,2,3 ]
y2 = [10,11,14]
[Link] (x2,y2,label=' second line')
[Link] ('plot number')
[Link] ('important variable')
[Link] ('new graph')
[Link] ()
[Link] ()

output-

[Link] gram to plot

frequency of marks using line chart.

import [Link] as plt

def fnplot(student,marks):

[Link](student,marks)

[Link]("marks line chart")

[Link]("no of student ")

[Link]("frequencyof marks")
[Link]()

marks=[50,50,50,65,65,75,75,80,80,90,90,90,90]

student=[1,2,3,4,5,6,7,8,9,10,11,12,13]

fnplot(student,marks)

output-

[Link]

plot multiple lines with different colours defined explicitly.

import [Link] as plt

import numpy as np

y=[Link](1,3)

[Link](y,'y')

[Link](y+1,'m')
[Link](y+2,'c')

[Link]()

output-

[Link] plot lines with different styles using plot ( ) functions.

import [Link] as plt


import numpy as np
y=[Link](1,3)
[Link](y,'--',y+1,'-.',y+2,'..')
[Link]()

output-

[Link] generate a histogram for displaying the number of students having

the same weight.

import [Link] as plt

student_weights=[19,25,30,40,39,55,60]
[Link](student_weight,bins=[0,10,20,30,40,50,50],weights=[20,10,15,35,4
0,6,8], edge colour='red')

[Link]()

output-

[Link] a

program to plot a bar chart to depict the changing weekly onion prices for

four weeks. Give appropriate axes labels.

Week = [1,2,3,4]

Price = [ 40,80,100,70]
import [Link] as plt

Week = [ 1,2,3,4]

Prices = [ 40,50,100,70]

[Link] ( week , prices)

[Link] (‘week’)

[Link] (‘onion prices’)

[Link]( )

[Link] two series element-by-element.

import pandas as pd
a = [Link] ([10,20,30,40,50])
b = [Link] ([10,25,30,35,60])
print ( “First Series is : ” )
print (a)
print ( “Second Series is : ”)
print (b)
print (“Comparing the elements of both the series ”)
print ( “Equals”)
print (a==b)
print ( “Greater than: ”)
print (a>b)
print (“Less than: ”)
print (a<b)

[Link] plot a bar chart for student strength analysis for three consecutive

year.

import pandas as pd

import [Link] as plt

plotdata=[Link]({"2019";[10,12,15,30],"2020":
[16,25,22,30],"2021":[19,22,29,32]} ,index=["A","B","C","D"])

[Link](kind="bar")
[Link]("student strenght analysis")

[Link]("sections")

[Link]("strength")

[Link]()

output-
output-

You might also like