Reputation: 4790
I have a dataframe like this
# initialize list of lists
data = [[1, ['ABC', 'pqr']], [2, ['abc', 'XY']], [3, np.nan]]
# Create the pandas DataFrame
data = pd.DataFrame(data, columns = ['Name', 'Val'])
data
Name Val
0 1 [ABC, pqr]
1 2 [abc, XY]
2 3 NaN
I am trying to convert every value in the list, to it's lower case
data['Val'] = data['Val'].apply(lambda x: np.nan if len(x) == 0 else [item.lower() for item in x])
data
However I get this error
TypeError: object of type 'float' has no len()
Expected final output
Name Val
0 1 [abc, pqr]
1 2 [abc, xy]
2 3 NaN
Upvotes: 0
Views: 92
Reputation: 862731
First idea is filter rows without missing values and processing:
m = data['Val'].notna()
data.loc[m, 'Val'] = data.loc[m, 'Val'].apply(lambda x: [item.lower() for item in x])
print (data)
Name Val
0 1 [abc, pqr]
1 2 [abc, xy]
2 3 NaN
Or you can processing only list
s filtered by isinstance
:
f = lambda x: [item.lower() for item in x] if isinstance(x, list) else np.nan
data['Val'] = data['Val'].apply(f)
print (data)
Name Val
0 1 [abc, pqr]
1 2 [abc, xy]
2 3 NaN
Upvotes: 1