More On DataFrame

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

More on DataFrame using Aggregate Funcition, Update Statement

Example : Consider the following Student DataFrame.

data={'RollNo':[1,2,3,4,5],

'Name':['Aditya','Balwant','Chirag','Deepak','Eva'],

'English':[23,18,27,11,17],

'Hindi':[20,1,23,3,21],

'Maths':[28,25,30,7,24]}

df=pd.DataFrame(data)

>>> df

RollNo Name English Hindi Maths


0 1 Aditya 23 20 28
1 2 Balwant 18 1 25
2 3 Chirag 27 23 30
3 4 Deepak 11 3 7
4 5 Eva 17 21 24

Q. To display the records of the students having roll numbers 2 and 3.

>>> df[1:3] or,

>>> df.loc[1:2,:] or,

>>> df.iloc[1:3,:]
RollNo Name English Hindi Maths
1 2 Balwant 18 1 25
2 3 Chirag 27 23 30

Q. To increase the marks of subject Math by 4, for all students.

>>> df['Maths’] = df['Maths']+4 or,

>>> df['Maths’] += 4

RollNo Name English Hindi Maths


0 1 Aditya 23 20 32
1 2 Balwant 18 1 29
2 3 Chirag 27 23 34
3 4 Deepak 11 3 11
4 5 Eva 17 21 28
More on DataFrame using Aggregate Funcition, Update Statement

Q. To display the Rollno and Name of all students who secured less than 15 marks
in Maths.

>>> df[df['Maths']<15] # Displaying all columns

RollNo Name English Hindi Maths


3 4 Deepak 11 3 11

>>> df['Maths']<15 # Returns Boolean values

0 False
1 False
2 False
3 True
4 False

>>> df['RollNo'] # Displaying single column

0 1
1 2
2 3
3 4
4 5

>>> df[['RollNo','Name']] # Display specific columns

RollNo Name
0 1 Aditya
1 2 Balwant
2 3 Chirag
3 4 Deepak
4 5 Eva

>>> df[df['Maths']<15][['RollNo','Name','Maths']] or,

>>> df[['RollNo','Name','Maths']][df['Maths']<15]

RollNo Name Maths


3 4 Deepak 11
More on DataFrame using Aggregate Funcition, Update Statement

Q. To add and display the total marks i.e., sum of marks secured in English, Hindi
and Maths for all students.

>>> df['Total'] = df['English']+df['Hindi']+df['Maths'] #Method 1

RollNo Name English Hindi Maths Total


0 1 Aditya 23 20 32 75
1 2 Balwant 18 1 29 48
2 3 Chirag 27 23 34 84
3 4 Deepak 11 3 11 25
4 5 Eva 17 21 28 66

#Method 2 : Using aggregate function – sum()

>>> df['Total']=df[['English','Hindi','Maths']].sum(axis=1)

RollNo Name English Hindi Maths Total


0 1 Aditya 23 20 32 75
1 2 Balwant 18 1 29 48
2 3 Chirag 27 23 34 84
3 4 Deepak 11 3 11 25
4 5 Eva 17 21 28 66

>>> df[['English','Hindi','Maths']].sum(axis=0)

English 96
Hindi 68
Maths 134
dtype: int64

Q. To add and display the average percentage of all students.

>>> df['Percent']=df['Total']/3 #Method 1

RollNo Name English Hindi Maths Total Percent


0 1 Aditya 23 20 32 75 25.00
1 2 Balwant 18 1 29 48 16.00
2 3 Chirag 27 23 34 84 28.00
3 4 Deepak 11 3 11 25 8.33
4 5 Eva 17 21 28 66 22.00
More on DataFrame using Aggregate Funcition, Update Statement

#Method 2 : Using aggregate function - mean()

>>> df['Percent’] = df[['English','Hindi','Maths']].mean(axis=1)

RollNo Name English Hindi Maths Total Percent


0 1 Aditya 23 20 32 75 25.00
1 2 Balwant 18 1 29 48 16.00
2 3 Chirag 27 23 34 84 28.00
3 4 Deepak 11 3 11 25 8.33
4 5 Eva 17 21 28 66 22.00

Q. Display the minimum or maximum marks of English, Hindi, Maths

>>> df[['English','Hindi','Maths']].min() or,

>>> df[['English','Hindi','Maths']].min(axis=0)

English 11
Hindi 1
Maths 11
dtype: int64

>>> df[['English','Hindi','Maths']].min(axis=1)

RollNo Name English Hindi Maths Total Percent


0 1 Aditya 23 20 32 75 25.00
1 2 Balwant 18 1 29 48 16.00
2 3 Chirag 27 23 34 84 28.00
3 4 Deepak 11 3 11 25 8.33
4 5 Eva 17 21 28 66 22.00

0 20
1 1
2 23
3 3
4 17
dtype: int64

Q. Display the student Name - 'Balwant'

>>> df.iloc[1,1] or,


More on DataFrame using Aggregate Funcition, Update Statement

>>> df.loc[1,'Name']

'Balwant'

Q. Update the student name 'Balwant' to 'Ram'

>>> df.iloc[1,1]='Ram' or,

>>> df.loc[1,'Name'] ='Ram'

RollNo Name English Hindi Maths Total


0 1 Aditya 23 20 28 71
1 2 Ram 18 1 25 44
2 3 Chirag 27 23 30 80
3 4 Deepak 11 3 7 21
4 5 Eva 17 21 24 62

Q. Update the marks of Hindi to 11 of 'Ram'

>>> df.iloc[1,3]=11 or,

>>> df.loc[1,'Hindi']=11

RollNo Name English Hindi Maths Total


0 1 Aditya 23 20 28 71
1 2 Ram 18 11 25 44
2 3 Chirag 27 23 30 80
3 4 Deepak 11 3 7 21
4 5 Eva 17 21 24 62

Q. Update the marks of Hindi to 44 of ‘Aditya' (in case of labelled indexing as


shown)

RollNo Name English Hindi Maths Total


A 1 Aditya 23 44 28 71
B 2 Ram 18 11 25 44
C 3 Chirag 27 23 30 80
D 4 Deepak 11 3 7 21
E 5 Eva 17 21 24 62
More on DataFrame using Aggregate Funcition, Update Statement

>>> df.index=['A','B','C','D','E'] #Rename index to labelled


index

>>> df.loc['A','Hindi']=44

You might also like