Reputation: 77
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 :
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
Reputation: 9217
You can use "G
reater than or e
qual" (.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 T
ranspose 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