gpt4 book ai didi

python - 单个 Dataframe 单元格中系列的最大值

转载 作者:行者123 更新时间:2023-11-30 21:57:24 25 4
gpt4 key购买 nike

我有一个数据框,其中每行在列中包含一个系列。

             col1    
row1 [34,55,11,8]
row2 [36,76,69,6]
row3 [77,31,40,55]
row4 [51,41,26,30]

我想获取系列中每个值的最大值并在新列中生成它。

        col1                   max 
row1 [34,55,11,8] 55
row2 [36,76,69,6] 76
row3 [77,31,40,55] 77
row4 [51,41,26,30] 51

我的尝试:

df['max'] = df['values'].apply(lambda x:x.max())
df.head()

错误

ValueError: zero-size array to reduction operation maximum which has no identity

最佳答案

使用数据框构造函数重新创建您的 list 类型列

df['Max']=pd.DataFrame(df['values'].tolist(),index=df.index).max(1)

关于python - 单个 Dataframe 单元格中系列的最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55265609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com