enter image description here In excel file i can do countif funtion like attached picture but How can i do this countif function in Python Pandas,please help me by providing the code
Asked
Active
Viewed 200 times
-5
-
This may help https://stackoverflow.com/questions/20995196/python-pandas-counting-and-summing-specific-conditions – Sevval Kahraman Sep 15 '20 at 16:10
-
2Welcome to SO. This isn't a discussion forum or tutorial. Please take the [tour] and take the time to read [ask] and the other links found on that page. [https://pandas.pydata.org/docs/user_guide/index.html](https://pandas.pydata.org/docs/user_guide/index.html) – wwii Sep 15 '20 at 16:15
1 Answers
0
Here is an example of how you can create a function to accomplish what you are looking for. The problem with the other stackoverflow posts suggested is you would have to reshape your data for those methods to work (which you could do with df.T), but that might not be practical in your situation.
I have updated my answer to account for the first column (name).
import pandas as pd
# creating example df
df = pd.DataFrame({'name':['Akbar','Jashim'],
'col1':[4,5],
'col2':[5,8],
'col3':[2,12],
'col4':[10,14],
'col5':[22,16],
'col6':[12,19],
'col7':[3,6],
})
# create function to get number of values
# over 10 for each row in the df
def countif(df):
over_10 = []
# Loop over rows in df using .iloc
for i in df.index:
row_count = (df.iloc[i,1:] > 10).sum()
over_10.append(row_count)
return over_10
df['over_10'] = countif(df)

Bradon
- 213
- 2
- 8
-
Dear Bradon sir,It works, thanks sir (Bradon) but one error occured .It shows, '>' not supported between instances of 'str' and 'int'. Because in first column there is a "name" column. Please help me with this name column,it is necessary. – Md Nur Rashed Khan Sep 15 '20 at 17:03
-
I've updated the above answer to account for the first column (name). I just added a little bit of code to the df.iloc[i] so that it grabs the row at index i and all the columns from index 1 until the end of the df. Now it looks like df.iloc[i,1:] – Bradon Sep 15 '20 at 17:30
-