1

I have a dataframe consisting of numeric strings and/or empty strings in multiple columns, and I would like to convert these columns from strings to "int" datatype. Before doing so, I want to convert the empty strings to "-1" (either the int or the string version of -1; it does not matter).

I am trying to simultaneously apply a lambda function to multiple columns to convert the empty string, but am getting an error "'The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().', 'occurred at index Temperature(F)'"

I am posting a dummy example below of what I'm trying to do with my actual dataframe, but it is not working. Of course there is a workaround of iterating through each column in a "for" loop, but I suspect there is a cleaner solution.

df = pd.DataFrame({'Temperature(F)':['30','40',''],'Gust':['','5','10']})
numericCols = ['Temperature(F)','Gust']
df[numericCols]=fTable[numericCols].apply(lambda x:-1 if x=='' else x)
df[numericCols] = fTable[numericCols].astype('int')
'''

As described, I get the error message "'The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().', 'occurred at index Temperature(F)'" when I run this.

1 Answer 1

1

In one line not using apply

df[numericCols].apply(pd.to_numeric, errors='coerce').fillna(value=-1)
# Out:
#    Temperature(F)  Gust
# 0            30.0  -1.0
# 1            40.0   5.0
# 2            -1.0  10.0
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.