Assignments IP Class 12
Assignments IP Class 12
Create Series
output:
Output:
----------------------------------------------
Assignment-
Series Functions/Methods
Suppose a series,
import pandas as pd
>>> s1 = pd.Series([11,22,33,None,44,55],['A','B',None,'D','E','F'])
Assignment-
Series Attributes
--------------------------------------------------------------------
-------------------------------------------------------------
Assignment-
Arithmetic Operation on panada Series:
Suppose four series s1,s2,s3 and s4
import pandas as pd
>>> s1 = pd.Series([11,22,33,44])
>>> s2 = pd.Series([111,222,333,444])
>>> s3 = pd.Series([11,22,33,44,55,66,77])
>>> s4 = pd.Series([111,222,333,444],index=[5,6,7,8])
>>> s1+s2
Output:
0 122
1 244
2 366
3 488
dtype: int64
OR
>>> s1.add(s2)
Output:
0 122
1 244
2 366
3 488
dtype: int64
---------------------------------------------------------
Assignment
Create DataFrame
1. Create a DataFrame to display total number of students
from class 9 to 12 of each section
import pandas as pd
ix= [55,59,54,57]
x = [54,55,59,60]
xi= [51,59,63,61]
xii= [52,62,57,50]
col = ['A','B','c','D']
ind = ['IX','X','XI','XII']
df=pd.DataFrame([ix,x,xi,xii],columns=col,index=ind)
print(df)
Output:
A B c D
IX 55 59 54 57
X 54 55 59 60
XI 51 59 63 61
XII 52 62 57 50
2. Create DataFrame from Dictionary of List
import pandas as pd
friends = { 'name': ['Ravi','Kavi','Chavi'],
'age': [38,34,37],
'cont': [123,234,345]
}
df = pd.DataFrame(friends)
print(df)
Output:
name age cont
0 Ravi 38 123
1 Kavi 34 234
2 Chavi 37 345
------------------------------------------------------------------
Assignment-
DataFrame Functions
Output:
name bst eco acc
0 Ravi 78.0 78 66
1 Kavi 65.0 74 78
2 Chavi NaN 75 85
>>> print(df.head(n=4))
Output:
name bst eco acc
0 Ravi 78.0 78 66
1 Kavi 65.0 74 78
2 Chavi NaN 75 85
3 Pavi 75.0 75 65
4 Amit 63.0 76 66
2. tail() # do yourself
3. count()
>>> print(df.count())
Output:
name 6
bst 5
eco 6
acc 6
dtype: int64
>>> print(df.count())
Output:
name Sumit
bst 78
eco 78
acc 85
dtype: object
-----------------------------------------------------------------
Assignment-
DataFrame attributes
>>> print(df.ndim)
Output:
2
2. size # gives size(rows*columns) of Dat
>>> print(df.size)
Output:
24
>>> print(df.shape)
Output:
(6,4)
Output:
RangeIndex(start=0, stop=6, step=1)
>>> print(df.values)
Output:
[['Ravi' 78.0 78 66]
['Kavi' 65.0 74 78]
['Chavi' nan 75 85]
['Pavi' 75.0 75 65]
['Amit' 63.0 76 66]
['Sumit' 65.0 67 61]]
6. dtype
---------------------------------------------------
Assignment-
Add Column and row in DataFrame
import panda as pd
friends = {
'name': ['Ravi','Kavi','Chavi'],
'bst' : [68,73,66],
'eco' : [78,74,75],
'acc' : [78,67,66]
}
df = pd.DataFrame(friends)
Output:
name bst eco acc eng
0 Ravi 68 78 78 65
1 Kavi 73 74 67 61
2 Chavi 66 75 66 69
2. Add/Insert row
>>> row = {'Name':'Pavi','bst':67,'Accountancy':78, 'eng': 70}
df = df.append(row,ignore_index=True)
print(df)
Output:
Name bst Accountancy eng
0 Ravi 68 78 65
1 Kavi 73 67 61
2 Chavi 66 66 69
3 Pavi 67 78 70
-------------------------------------------------------------------
Assignment-
Delete Column and Row
import panda as pd
friends = {
'name': ['Ravi','Kavi','Chavi'],
'bst' : [68,73,66],
'eco' : [78,74,75],
'acc' : [78,67,66]
}
1. Delete row:
>>> df = df.drop(1)
print(df)
Output:
name age cont Blood_Gp
0 Ravi 38 123 B+
2 Chavi 37 345 AB+
2. Delete row
df.pop('age')
print(df)
output:
df=df.drop('cont',axis=1)
print(df)
output:
name Blood_Gp
0 Ravi B+
2 Chavi AB+
--------------------------------------------------
Assignment-
import pandas as pd
friends = { 'name': ['Ravi','Kavi','Chavi'],
'age': [38,34,37],
'cont': [123,234,345],
'Blood_Gp':['B+','A+','AB+']
}
df = pd.DataFrame(friends)
df.to_csv('H:/My Drive/Project/friends.csv')
output of CSV
df=pd.read_csv('H:/My Drive/Project/friends.csv')
print(df)
Output:
name age cont Blood_Gp
0 Ravi 38 123 B+
1 Kavi 34 234 A+
2 Chavi 37 345 AB+