DD08
DD08

Reputation: 77

count occurance of value in each column of pandas

I have a requirement where I need to get a count of all values greater than 80 in all columns of pandas.

code:

import pandas as pd

data = {
   'name':['a','b','c'] ,
  "4-8-2021": [80,60,70],
  "5-8-2021": [50, 40, 45],
  "6-8-2021": [70,60,50],
  "7-8-2021": [110, 99, 45]
    
}

df = pd.DataFrame(data)
df

count=df[df.columns[1:] < 99.99].count() 
count

I am getting the error "TypeError: '<' not supported between instances of 'str' and 'float'"

The output I wanted is :

enter image description here

Is there any other efficient way to achieve this? It will be grateful if someone can help to solve the issue. Thank you in advance

Upvotes: 0

Views: 58

Answers (1)

Andreas
Andreas

Reputation: 9217

You can use "Greater than or equal" (.ge):

s = df.iloc[:, 1:].ge(80).sum()

4-8-2021    1
5-8-2021    0
6-8-2021    0
7-8-2021    2
dtype: int64

This is a pd.Series, which if you want to attach to a dataframe e.g. as a row you have to change the format to_frame (dataframe), then Transpose it, and then finally, u can .append() it.

df.append(s.to_frame().T)

  name  4-8-2021  5-8-2021  6-8-2021  7-8-2021
0    a        80        50        70       110
1    b        60        40        60        99
2    c        70        45        50        45
0  NaN         1         0         0         2

Upvotes: 1

Related Questions