import pandas as pd
my_dict={'NAME':['Ravi','Raju','Alex'],
'ID':[1,2,3],
'MATH':[80,40,70]
}
df = pd.DataFrame(data=my_dict) # crate DataFrame
print(df.MATH.sum()) # 190
print(df.MATH.max()) # 80
print(df.MATH.min()) # 40
print(df.MATH.mean()) # 63.33
print(df.MATH.mode()) #
print(df.MATH.median()) # 70.0
We will read data from one excel file ( student.xlsx ) by using read_excel() to create a DataFrame.
import pandas as pd
my_data = pd.read_excel('student.xlsx')
print(my_data)
This will return all the rows.
sum of all marks of 4th class.
import pandas as pd
my_data=pd.read_excel("student.xlsx")
my_data=my_data[my_data.my_class.str.contains('Four')]
print(my_data.mark.sum())
Output is
638
Maximum mark of 3rd class
import pandas as pd
my_data=pd.read_excel("student.xlsx")
my_data=my_data[my_data.my_class.str.contains('Three')]
print(my_data.mark.max())
Output is
85
Maximum mark is 85 but who is the student got the highest mark ?
import pandas as pd
my_data=pd.read_excel("student.xlsx")
my_data=my_data[my_data.my_class.str.contains('Three')]
print(my_data.mark.max())
print(my_data[my_data.mark == my_data.mark.max()])
Output
85
ID name my_class mark sex
1 2 Max Ruin Three 85 male
Let us try getting lowest ( Minimum ) value of mark in class
import pandas as pd
my_data=pd.read_excel("student.xlsx")
my_data=my_data[my_data.my_class.str.contains('Three')]
print(my_data.mark.min())
Output
55
The matching row or get the row with lowest mark.
import pandas as pd
my_data=pd.read_excel("student.xlsx")
my_data=my_data[my_data.my_class.str.contains('Three')]
print(my_data.mark.min())
print(my_data[my_data.mark == my_data.mark.min()])
Output
55
ID name my_class mark sex
2 3 Arnold Three 55 male
Mean value of marks of class three
import pandas as pd
my_data=pd.read_excel("student.xlsx")
my_data=my_data[my_data.my_class.str.contains('Three')]
print(my_data.mark.mean())
Similarly we can find out other values .
print(my_data.mark.median())
print(my_data.mark.mode())
idxmax()
We can get id of the row with maximum value ( not the maximum value itself )
import pandas as pd
my_data=pd.read_excel("student.xlsx")
my_data=pd.DataFrame(my_data)
print(my_data.mark.idxmax())
print(my_data[my_data.ID == my_data.mark.idxmax()])
Output
32
ID name my_class mark sex
31 32 Binn Rott Seven 90 female
idxmin()
We can get id of the row with minimum value
import pandas as pd
my_data=pd.read_excel("student.xlsx")
my_data=pd.DataFrame(my_data)
print(my_data.mark.idxmin())
print(my_data[my_data.ID == my_data.mark.idxmin()])
Output
18
ID name my_class mark sex
17 18 Honny Five 75 male
count()
Counting number of records
import pandas as pd
my_data=pd.read_excel("student.xlsx")
my_data=my_data[my_data.my_class.str.contains('Three')]
print(my_data.mark.count())
Output
3
« str.contains
« Pandas
read_csv()
read_excel()
to_excel()
← Subscribe to our YouTube Channel here