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

Create A Data Frame

Uploaded by

dassneha064
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)
8 views25 pages

Create A Data Frame

Uploaded by

dassneha064
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
You are on page 1/ 25

Data frame

01-Jul-24 Onkar Singh Rathore 1


01-Jul-24 Onkar Singh Rathore 2
01-Jul-24 Onkar Singh Rathore 3
Syntax of Data Frame

01-Jul-24 Onkar Singh Rathore 4


01-Jul-24 Onkar Singh Rathore 5
Create a DataFrame by Dictionary of List
Import pandas as pd
d1={2020:[100,99,100,96],2019:[100,100,98,88],2018:[100,98,100,88]}
df1=pd.DataFrame(d1,index=['IP','CS','EG','PE'])
print(df1)

2020 2019 2018


IP 100 100 100
CS 99 100 98
EG 100 98 100
PE 96 88 88

01-Jul-24 Onkar Singh Rathore 6


DataFrame by Dictionary of List

import pandas as pd
l1=[100,99,100,96]
l2=[100,100,98,88]
l3=[100,98,100,88]
d1={2020:l1,2019:l2,2018:l3}
df1=pd.DataFrame(d1,index=['IP','CS','EG','PE'])
print(df1)

2020 2019 2018


IP 100 100 100
CS 99 100 98
EG 100 98 100
PE 96 88 88

01-Jul-24 Onkar Singh Rathore 7


Create a DataFrame by dictionary of List

import pandas as pd
l1=[100,99,100,96]
l2=[100,100,98,88]
l3=[100,98,100,88]
d1={2020:l1,2019:l2,2018:l3}
df1=pd.DataFrame(d1)
print(df1)

2020 2019 2018


0 100 100 100
1 99 100 98
2 100 98 100
3 96 88 88

01-Jul-24 Onkar Singh Rathore 8


Create a DataFrame by Nested Dictionary

import pandas as pd
d1={'IP':100, 'CS':100,'EG':98,'PE':78}
d2={'IP':98, 'CS':100,'EG':99,'PE':78}
d3={'IP':92, 'CS':91,'EG':93,'PE':87}
d4={2020:d1, 2019:d2,2018:d3}
df1=pd.DataFrame(d4)
print(df1)

2020 2019 2018


IP 100 98 92
CS 100 100 91
EG 98 99 93
PE 78 78 87

01-Jul-24 Onkar Singh Rathore 9


Create a DataFrame by Dictionary of Series

import pandas as pd
d1={2020:pd.Series([100,99,100,96]),
2019:pd.Series([100,100,98,88] )
,2018:pd.Series([100,98,100,88] )}
df1=pd.DataFrame(d1)
print(df1)

2020 2019 2018


0 100 100 100
1 99 100 98
2 100 98 100
3 96 88 88

01-Jul-24 Onkar Singh Rathore 10


Create a DataFrame by List of Lists

0 1 2
0 1 2 3
1 4 5 6
2 7 8 9

01-Jul-24 Onkar Singh Rathore 11


Create a DataFrame by Lists of List

import pandas as pd
x=[[1,2,3],[4,5,6],[7,8,9]]
y=pd.DataFrame(x,index=["A","B","C"],columns=["M1","M2","M3"])
print(y)

M1 M2 M3
A 1 2 3
B 4 5 6
C 7 8 9

01-Jul-24 Onkar Singh Rathore 12


import pandas as pd
S=pd.Series([1,2,3,4,5])
print(S)

d=pd.DataFrame(S)
print(d)

01-Jul-24 Onkar Singh Rathore 13


0 1
1 2
2 3
3 4
4 5
dtype: int64

0
0 1
1 2
2 3
3 4
4 5

01-Jul-24 Onkar Singh Rathore 14


Create a DataFrame by List of Dictionary

import pandas as pd
l=[{'a':1,'b':2},{'a':3,'b':4}]
d=pd.DataFrame(l)
print(d)

a b
0 1 2
1 3 4

01-Jul-24 Onkar Singh Rathore 15


Create a DataFrame by List of Dictionary

01-Jul-24 Onkar Singh Rathore 16


import pandas as pd
l=[{'Name':'Ram','Roll No':2},{'Name':'Ram','Roll
No':3,'Salary':3000}]
d=pd.DataFrame(l)
print(d)

01-Jul-24 Onkar Singh Rathore 17


Name Roll No Salary
0 Ram 2 NaN
1 Ram 3 3000.0

01-Jul-24 Onkar Singh Rathore 18


import pandas as pd
l={"DAV":{'Name':'Ram','Roll
No':2},"KPS":{'Name':'Ram','Roll No':3,'Salary':3000}}
d=pd.DataFrame(l)
print(d)

01-Jul-24 Onkar Singh Rathore 19


DAV KPS
Name Ram Ram
Roll No 2 3
Salary NaN 3000

01-Jul-24 Onkar Singh Rathore 20


import pandas as pd
l=[[1,2,3,4,5],[1,2,3,4,5]]
d=pd.DataFrame(l)
print(d)

01-Jul-24 Onkar Singh Rathore 21


0 1 2 3 4
0 1 2 3 4 5
1 1 2 3 4 5

01-Jul-24 Onkar Singh Rathore 22


Create a DataFrame by Numpy array

import pandas as pd
import numpy as np
l=np.array([[1,2,3,4,5],[1,2,3,4,5]])
d=pd.DataFrame(l)
print(d)

01-Jul-24 Onkar Singh Rathore 23


Create a DataFrame by another DataFrame

import pandas as pd
d1={2020:[100,99,100,96],2019:[100,100,98,77],2018:[100,98,100,88]}
df1=pd.DataFrame(d1,index=['IP','CS','Math','English'])
df2=pd.DataFrame(df1)
print(df2)

2020 2019 2018


IP 100 100 100
CS 99 100 98
Math 100 98 100
English 96 77 88

01-Jul-24 Onkar Singh Rathore 24


df1=pd.read_csv("d:/davcsv.csv")
print(df1)

01-Jul-24 Onkar Singh Rathore 25

You might also like