Home > Blockchain >  How to COUNTIF with Python?
How to COUNTIF with Python?

Time:11-25

I have an excel sheet with 14 columns. The first two columns contain names of people and their adress and the other twelve columns contain the word "approved" or "not approved". I know the function COUNTIF in Excel. Is there a way to count how many "approved" there are with Python? This is my code to read my dataframe:

df = pd.read_excel("Members.xlsx", sheet_name="eliminations")

I tried this:

print(sum(df = "approved"))

But this is my error: TypeError: sum() takes at least 1 positional argument (0 given)

CodePudding user response:

You are close, need np.sum with convert values to numpy array:

np.random.seed(2021)
df = pd.DataFrame(np.hstack((np.random.choice([1,2], size=(3, 2)), 
                             np.random.choice(['approved','not approved'], size=(3, 4)))))
print (df)
   0  1             2             3             4             5
0  1  2      approved      approved  not approved      approved
1  2  1  not approved  not approved  not approved  not approved
2  2  1      approved      approved      approved      approved

print(np.sum(df.to_numpy() == "approved"))
7

If ned select from secon dto last columns add DataFrame.iloc:

print(np.sum(df.iloc[:, 2:].to_numpy() == "approved"))
7
  • Related