Yabin Da
Yabin Da

Reputation: 595

How can I check whether a column contains strings in pandas

The codes are below. I want to create a new column "value_c" based on simple operation on column "value".

frame['value_c'] = frame['value'].apply(lambda x: (x-32) / (5/9))

However, I got an error message like below.

TypeError: unsupported operand type(s) for -: 'str' and 'int'

How can I check whether the column has strings and how can I delete them?

Thank you!!!

Upvotes: 2

Views: 98

Answers (1)

U13-Forward
U13-Forward

Reputation: 71560

Use pd.to_numeric then dropna:

frame['value_c'] = pd.to_numeric(frame['value'],errors='coerce').dropna().apply(lambda x: (x-32) / (5/9))

Then it will work as expected.

Upvotes: 1

Related Questions