0% found this document useful (0 votes)
4 views18 pages

Python Pandas Series and DataFrame Examples

The document contains a series of Python programs demonstrating the use of pandas and matplotlib for data manipulation and visualization. It includes examples of creating pandas Series and DataFrames, performing operations like grouping and quantile calculations, and generating various types of plots such as bar charts and histograms. Each program is accompanied by its output, showcasing the results of the operations performed.

Uploaded by

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

Python Pandas Series and DataFrame Examples

The document contains a series of Python programs demonstrating the use of pandas and matplotlib for data manipulation and visualization. It includes examples of creating pandas Series and DataFrames, performing operations like grouping and quantile calculations, and generating various types of plots such as bar charts and histograms. Each program is accompanied by its output, showcasing the results of the operations performed.

Uploaded by

archarejitha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

write a program to create a pandas series using dictionary of values and nd


array

import pandas as pd
import numpy as np
a=[Link]([2,3,4])
s=[Link](a)
print(s)
d={'a':2,'b':3,'c':4}
S1=[Link](d)
print(S1)
Output
0 2
1 3
2 4
dtype: int64
a 2
b 3
c 4
dtype: int64
[Link] a python program to create a series using the range and arrange function

import pandas as pd
import numpy as np
b=[Link](4,8)
s=[Link](index=b,data=b*2)
print(s)
s1=[Link](45,index=range(10))
print(s1)

Output
4 8
5 10
6 12
7 14
dtype: int64
0 45
1 45
2 45
3 45
4 45
5 45
6 45
7 45
8 45
9 45
dtype: int64
3. create a series using mathematical expressions

import pandas as pd
import numpy as np
c=[Link](3,8)
s=[Link](index=c,data=c+4)
print(s)

Output
3 7
4 8
5 9
6 10
7 11
dtype: int64
4. write a python program to generate a series , print all the elements that are
above 75th percentile

import pandas as pd
import numpy as np
s=[Link]([Link]([14,24,18,45,17,13,14,46]))
print('element of series')
print(s)
print()
print('75''percentile of series element')
d=[Link]([0.75])
print(d)
print()
print('the element above 75th percentile in the series')
print(s[s>([Link](0.75))])

Output
element of series
0 14
1 24
2 18
3 45
4 17
5 13
6 14
7 46
dtype: int64
75percentile of series element
0.75 29.25
dtype: float64
the element above 75th percentile in the series
3 45
7 46
dtype: int64
5. write a program to create two series and perform basic subtraction operation

import pandas as pd
s1=[Link](data=[10,20,30],index=[1,2,3])
s2=[Link](data=[40,50,60],index=[1,2,3])
s3=[Link]()
s3=s2-s1
print(s1)
print(s2)
print(s3)

Output

1 10
2 20
3 30
dtype: int64
1 40
2 50
3 60
dtype: int64
1 30
2 30
3 30
dtype: int64
6. create a dataframe using list of index and column

import pandas as pd
L=[[2,45],[3,37],[4,46],[5,28]]
df=[Link](L,index=['a','b','c','d'],columns=['rollno','mark'])
print (df)
output
rollno mark
a 2 45
b 3 37
c 4 46
d 5 28
[Link] a program to create a dataframe using list of dictionary

import pandas as pd
A=[{'name':'a','mark':35},{'name':'b','mark':42},{'name':'c','mark':46}]
dt=[Link](A)
print(dt)
Output
name mark
0 a 35
1 b 42
2 c 46
[Link] a dataframe of a quarterly sales were each rows, contains the item,
category, item name and expenditure, group the rows by category and print the
total expenditure per category

import pandas as pd
di={'category':['tv','washingmachine','aircon','phone'],

'name':['lg','sony','samsung','apple'],'expenditure':[30000,23000,40000,80000]}
print('the created DataFrame')
df=[Link](di)
print('the total expenditure')
q=[Link]('category')
print(q['expenditure'].sum())

Output
the created DataFrame
the total expenditure
category
aircon 40000
phone 80000
tv 30000
washingmachine 23000
Name: expenditure, dtype: int64
[Link] a dataframe for an examination result which displays the row labels,
column labels and datatype of each columns and the dimensions

import pandas as pd
dic={'year':(2022,2021,2020,2019,2018,2017,2016),
'result':(100,99,99.5,99.4,99.8,99.5,100.0)}
df=[Link](dic)
print('element of the dataframe:')
print(df)
print([Link])
print('shapes of the dataframe:')
print([Link])

Output
element of the dataframe:
year result
0 2022 100.0
1 2021 99.0
2 2020 99.5
3 2019 99.4
4 2018 99.8
5 2017 99.5
6 2016 100.0
Year int 64
Result int 64
[Link] a program to display the first two rows and last two rows from a
dataframe

import pandas as pd
d={'rollno':[1,2,3,4,5,5],'Name':['a','b','c','d','e','f'],
'mark':[90,80,70,77,82,83]}
df=[Link](d)
print(df)
print([Link](2))
print([Link](2))
Output
rollno Name mark
0 1 a 90
1 2 b 80
2 3 c 70
3 4 d 77
4 5 e 82
5 5 f 83
rollno Name mark
0 1 a 90
1 2 b 80
rollno Name mark
4 5 e 82
5 5 f 83
[Link] and export data between pandas and csv file .

import [Link] as plt


import pandas as pd
df= pd.read_csv('C:\\Users\\User\\Desktop\\[Link]')
print('The Dataframe')
print(df)

Output
The Dataframe
roll no name mark
0 1 aksa 80
1 2 josh 90
2 3 martin 75
3 4 darwin 60
12. Create a python program that replaces all the negative values in a dataframe
with zero.

import pandas as pd
dic={'data1':[5,-4,6,7,-1,10],
'data2':[1,-2,4,-8,-6,-3]}
df=[Link](dic)
print(df)
print()
print('dataframe after replacing negative values with zero..')
df[df>0]=0
print(df)
output:
data1 data2
0 5 1
1 -4 -2
2 6 4
3 7 -8
4 -1 -6
5 10 -3
dataframe after replacing negative values with zero..
data1 data2
0 0 0
1 -4 -2
2 0 0
3 0 -8
4 -1 -6
5 0 -3
[Link] a python program to analyze a school record data and create a barchart
based on the above data set
import [Link] as plt
import pandas as pd
df=pd.read_csv("C:\\Users\\User\\Desktop\\[Link]")
print(' the dataframe')
print(df)
[Link](kind='bar')
[Link]()

output
the dataframe
Unnamed: 0 rollno mark subject
0 1 1 80 ip
1 2 2 50 ip
2 3 3 60 cs
[Link] a barchart with the help of dataframe and complete the chart with
appropriate chart components

import pandas as pd
import [Link] as plt
df=[Link]({2019:[10,20,15,30],2018:[16,25,22,30]},index=['A','B','C','D
'])
[Link](kind='bar',color=['r','b'])
[Link]('students strength')
[Link]()

OUTPUT
[Link] a student histogram based on their percentage of marks

import [Link] as plt


marks=[90,50,40,60,55,44,30,10,34,84]
grade_intervals=[0,35,70,100]
[Link]('student grade')
[Link](marks,grade_intervals)
[Link]([0,35,70,100])
[Link]('percentage')
[Link]('number of students')
[Link]()

Output
16. write a program to create a barchart based on the student strength on section
wise and show their percentage of attendance on different years like 2018,2019
and 2020

import pandas as pd
import [Link] as plt
df=[Link]({2018:[60,70,82],2019:[78,87,98],
2020:[76,75,85]},index=['A','B','C'])
[Link](kind='bar')
[Link]('percentage of attendence')
[Link]('x')
[Link]('y')
[Link]()

OUTPUT
[Link] a program to create a line chart that shows the result of a cricket match

import [Link] as plt


[Link]([10,20,30,40,50],[45,25,50,75,100],color='b',marker='*')
[Link]('cricket match record')
[Link]('over')
[Link]('runs')
[Link]()

OUTPUT

You might also like