INFORFMATICS PRACTICES - NOTES – Part 1
CLASS 12 (2024-25)
Chapter – PYTHON SERIES ,DATAFRAMES & VISUALISATION
To use series / dataframe : import pandas as pd
Series : DataFrame:
Basic feature of series are : Basic feature of DataFrame are :
a. Homogeneous data a. Heterogeneous data
b. Size Immutable b. Size Mutable
c. Values of Data Mutable c. Data Mutable
Syntax :- [Link]( data, index) [Link](d1, index=[2,3],columns=['name','age'])
Empty Series: Empty DataFrame:
import pandas as pd import pandas as pd
s = [Link]() df1=[Link]()
print(s) print(df1)
import pandas as pd
L = [['abc', 15], ['def', 16], ['ghi', 17]]
df1=[Link](L, columns=['name', 'age'])
print(df1)
o/p: name age
0 abc 15
1 def 16
2 ghi 17
Create a Series from Scalar e.g: import pandas as pd1
import pandas as pd1 data1 = [1,2,3,4,5]
s = [Link](5, index=[0, 1, 2, 3]) df1 = [Link](data1)
print(s) print (df1)
Output 0 5 1 5 2 5 3 5 import pandas as pd1
dtype: int64 data1 = [['Freya',10],['Mohak',12],['Dwivedi',13]]
Note :- here 5 is repeated for 4 times (as per no of index) df1 = [Link](data1,columns=['Name','Age'])
print (df1)
import pandas as pd1
data1 = {'Name':['Freya', 'Mohak'],'Age':[9,10]}
df1 = [Link](data1)
print (df1)
Output Name Age
0 Freya 9
1 Mohak 10
d1 = {'name':['abc', 'def', 'ghi'], 'age':[15,16,17] }
df1 = [Link](d1)
print(df1)
import pandas as pd1 Creating DataFrame From a CSV ( Comma Separated Value) :
s = [Link]([1,2,3]) data = pd.read_csv("[Link]")
t = [Link]([1,2,4]) Column addition :
u=s+t #addition operation 02 df = [Link]({"A": [1, 2, 3], "B": [4, 5, 6]})
print (u) 14 c = [7,8,9]
u=s*t # multiplication operation 27 dtype: int64 df[‘C'] = c Column Deletion :
print (u) 01
# Deleting column del df1['one']
14
[Link]('two')
2 12 dtype: int64
Functions :
Head function:
import pandas as pd1
s = [Link]([1,2,3,4,5],index = ['a','b','c','d','e'])
print ([Link](3)) Output: a 1
b. 2
Return first 3 elements c. 3 dtype: int64
tail function
import pandas as pd1
s = [Link]([1,2,3,4,5],index = ['a','b','c','d','e']) print
([Link](3)) Output c 3
d. 4
Return last 3 elements e. 5 dtype: int64
Loc: gets rows (or columns) with particular labels Loc: (selection by location (index) )
from the index. [Link][:3] print ([Link]['b'])
Output one 2.0
iloc : gets rows (or columns) at particular positions two 2.0
in the index (so it only takes integers). > [Link][:3] iloc :(Selection by integer location)
import pandas as pd1 d1 = {'one' : [Link]([1, 2, 3], index=['a', 'b',
'c']), 'two' : [Link]([1, 2, 3, 4], index=['a', 'b', 'c','d'])}
df1 = [Link](d1) Output
print ([Link][2]) one 3.0
two 3.0
Using multiple indexes :
d={'a':101, 'b':102, 'c':103, 'd':104, 'e':105, 'f':106}
s=[Link](d)
u=s[['b', 'a', 'f']] import pandas as pd1
print(u) s = [Link]([1,2,3,4,5],index =
o/p: ['a','b','c','d','e'])
b 102
print (s[['c','d']])
a 101
f 106 Output c 3
d 4
DataFrames : Concatenating 2 dataframes : [Link]([df1, df2])
Rename columns:
df = [Link]({"A": [1, 2, 3], "B": [4, 5, 6]})
[Link](columns={"A": "a", "B": "c"})
a c
01 4
12 5
23 6
DATA VISUALISATION :
[Link]()
[Link](data,bins=10)
[Link]() #bin edges not shown automatically
Output: