Chapter 10 Python Pandas
Chapter 10 Python Pandas
Chapter 10 Python Pandas
Informatics
Practices
Class XI ( As per
Python
CBSE Board)
Pandas
1. Array
2. Dict
3. Scalar value or constant
e.g.
Output
Series([], dtype: float64)
Output Output
0 a 100 a
1 b 101 b
2 c 102 c
3 d 103 d
dtype: object dtype: object
Note : default index is starting
from 0 Note : index is starting from 100
Output Output
a 0.0 b 1.0
b 1.0 c 2.0
c 2.0 d NaN
dtype: float64 a 0.0
dtype: float64
Output
0 5
1 5
2 5
3 5
dtype: int64
Note :- here 5 is repeated for 4 times (as per no of index)
Visit : python.mykvs.in for regular updates
Python Pandas
Pandas Series
Accessing Data from Series with Position
e.g.
import pandas as pd1
s = pd1.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
print (s[0])# for 0 index position
print (s[:3]) #for first 3 index values
print (s[-3:]) #for last 3 index values
Output
1
a 1
b 2
c 3
dtype: int64
c 3
d 4
e 5
dtype: int64
Output
c 3
d 4
dtype: int64
Output
a 1
b 2
c 3
dtype: int64
Return first 3 elements
Output
c 3
d 4
e 5
dtype: int64
Return last 3 elements
Rows
e.g.2
import pandas as pd1
data1 = [['Freya',10],['Mohak',12],['Dwivedi',13]]
Name Age
df1 = pd1.DataFrame(data1,columns=['Name','Age'])
0 Freya 10
print (df1) output 1 Mohak 12
2 Dwivedi 13
Output
Name Age
0 Freya 9
1 Mohak 10
Output
x y z
0 1 2 NaN
1 5 4 5.0
Output
one 2.0
two 2.0
Name: b, dtype: float64
Output
one 3.0
two 3.0
Name: c, dtype: float64
df1 = df1.append(df2)
print (df1)
Deletion of Rows
# Drop rows with label 0
df1 = df1.drop(0)
Output
freya 10
mohak 1
Output
freya 10
mohak 1
Output
0 1 2
0 1 4 7
1 4 10 16
2 9 18 27
Output
0 1 2
0 2 8 14
1 4 10 16
2 6 12 18
Note :- similarly we can use sub,mul,div functions
Visit : python.mykvs.in for regular updates
Python Pandas
Pandas DataFrame
Output
add 88 1
radd 99 1
add 88 <__main__.Commuter object at 0x02181370>
Output
add 88 1
radd 99 1
add 88 <__main__.Commuter object at 0x02181370>
OUTPUT
name age
0 freya 10
Output
Greater than:
0 True
1 True
2 True
3 True
4 False
dtype: bool
Visit : python.mykvs.in for regular updates
Python Pandas
Pandas DataFrame
Merging/combining dataframe
e.g.
import pandas as pd
left = pd.DataFrame({
'id':[1,2],
'Name': ['anil', 'vishal'],
'subject_id':['sub1','sub2']})
right = pd.DataFrame(
{'id':[1,2],
'Name': ['sumer', 'salil'],
'subject_id':['sub2','sub4']})
print (pd.merge(left,right,on='id'))
Output
id Name_x subject_id_x Name_y subject_id_y
0 1 anil sub1 sumer sub2
1 2 vishal sub2 salil sub4
Visit : python.mykvs.in for regular updates
Python Pandas
Pandas DataFrame
Merging/combining dataframe(different styles)