Reputation: 33
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.
Upvotes: 1
Views: 45
Reputation: 29307
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
Upvotes: 1