Home > OS >  Counting the filtered values in Pandas Python
Counting the filtered values in Pandas Python

Time:01-03

col1  col2   col3
0     14     37
10    29     49
20    30     40

I want to know the number of filtered values only on single column.Such as how many numbers are greater than 45 in column 3. I tried

a = df["col3"] <= 45
a.sum()

But the out was 0. (This is an example of the actual data I am working on.My actual data has 10.000 rows and 100 columns)

CodePudding user response:

A simple way to do it would be:

len(df[df['col3'] <= 45])

CodePudding user response:

d = {"col1": [0, 10, 20], "col2": [14, 29, 30], "col3": [37, 49, 40]}
df = pd.DataFrame(d)
print(df)
df_filter = df[(df["col3"] <= 45)]
print("col3 sum\n",df_filter["col3"].sum()) # col3 data sum


print("---"*50)
print("all columns sum\n",df_filter.sum())# all data sum
  • Related