Salih
Salih

Reputation: 719

Pandas Counting Each Column with its Spesific Thresholds

If I have a following dataframe:

          A       B       C       D       E

1         1       2       0       1       0
2         0       0       0       1      -1
3         1       1       3      -5       2
4        -3       4       2       6       0
5         2       4       1       9      -1
T         1       2       2       4       1

The last row is my threshold values for each column. I want to count each column values whether lower its threshold values or not in python pandas.

Desired Output is;

         A       B       C       D       E
Count    2       2       3       3       4

But, I need to figure it out with a general solution, not for these specific columns. Because I have a large dataset. I cannot specify a column name for each of them in the code.

Could you please help me with this?

Upvotes: 1

Views: 129

Answers (1)

jezrael
jezrael

Reputation: 863711

Select all rows without first by indexing and compare by DataFrame.lt by last row, then sum and convert Series to one row DataFrame by Series.to_frame with transpose by DataFrame.T:

df = df.iloc[:-1].lt(df.iloc[-1]).sum().to_frame('count').T
print (df)
       A  B  C  D  E
count  2  2  3  3  4

Numpy alternative with DataFrame constructor:

arr = df.values
df = pd.DataFrame([np.sum(arr[:-1] < arr[-1], axis=0)], columns=df.columns, index=['count'])
print (df)
       A  B  C  D  E
count  2  2  3  3  4

Upvotes: 4

Related Questions