首页 > 解决方案 > pandas:获取带有字符串数字的列表的最大值

问题描述

我有一个数据框df=pd.DataFrame({'xx':[['100','5','3'], ['5','40'], ['100']]}),我想将每个列表的最大值作为一个数字。所以我想得到这个:

xx
0   100
1   40
2   100

有没有办法做到这一点?

标签: stringpandaslistmax

解决方案


将值转换为整数并获取max值:

df['xx'] = df['xx'].map(lambda x: max(int(y) for y in x))

或使用列表理解:

df['xx'] = [max(int(y) for y in x) for x in df['xx']]

print(df)
    xx
0  100
1   40
2  100

推荐阅读