FunLearning68
FunLearning68

Reputation: 11

Type error: "not supported between instances of 'str' and 'float"?

I was unable to group the values in column "MinTemp" into 3 groups and update the dataframe.

The values in column "MinTemp" range from -8.2 to 33.9.
I only want to have 3 groups, <= 10.0 (mintp1), > 10.0 && <= 22.0 (mintp2) and > 22.0 (mintp3).

from collections import Counter

col         = 'MinTemp'

conditions  = [ data_mod[col] > 22.0, (data_mod[col] > 10.0) & (data_mod[col] <= 22.0), data_mod[col] <= 10.0 ]

choices     = [ 'mintp3', 'mintp2', 'mintp1' ]
data_mod["MinTemp"] = np.select(conditions, choices, default='neutral')

Counter(MinTemp)

TypeError: '>' not supported between instances of 'str' and 'float'

Upvotes: 1

Views: 3800

Answers (1)

DSC
DSC

Reputation: 1153

Use pd.to_numeric to convert the string values to numeric values allowing comparison:

df[col] = pd.to_numeric(df[col])

Upvotes: 1

Related Questions