熊猫:单元格列表的每个值均小写

时间:2020-05-05 07:38:52

标签: python-3.x pandas dataframe

我有一个这样的数据框

# 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

我正在尝试将列表中的每个值都转换为小写

data['Val'] = data['Val'].apply(lambda x: np.nan if len(x) == 0 else [item.lower() for item in x])
data

但是我收到此错误

TypeError: object of type 'float' has no len()

预期的最终产量

     Name  Val
0   1     [abc, pqr]
1   2     [abc, xy]
2   3     NaN

1 个答案:

答案 0 :(得分:1)

第一个想法是在不丢失值和进行处理的情况下筛选行:

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

或者您也只能处理list过滤的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