str.contains.sum()

Pandas str.contains()

Getting sum, maximum, mininum vlaues of any column

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 vlaue ( 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()


plus2net.com



Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer