2

I am trying to use the following code to set a new column, wdf['Thunder'] to the value of wdf['RngT] if the word Thunderstorm is in the column wdf['Notes']. However, I keep getting the error TypeError: argument of type 'float' is not iterable. None of the columns are floats. RngT is an int, Notes is an object. Full code is:

wdf['Thunder'] = [wdf['RngT'] if 'Thunderstorm' in x else 0 for x in wdf['Notes']]

New to pandas, so would appreciate any insight.

Thanks!

1 Answer 1

1

You may consider a performant alternative, np.where:

wdf['Thunder'] = np.where(
    wdf['Notes'].astype(str).str.contains('Thunderstorm'), wdf['RngT'], 0
)

Note that your first approach probably didn't work because your column was mixing strings and floats. Using astype(str) before checking for containment should fix that.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.