gpt4 book ai didi

python - 从列中的字符串中提取每行的最大值

转载 作者:太空宇宙 更新时间:2023-11-04 11:15:16 24 4
gpt4 key购买 nike

我在 DataFrame 中有一列字符串,其中包含以逗号分隔的数字。我需要从字符串中提取每一行的最大值。返回的最大值应该是从头开始到第13个索引的最大值。

我尝试使用“,”作为分隔符来拆分字符串,将其转换为启用扩展选项的列表。然后我使用 Pandas 的 assign 方法沿垂直轴查找最大值。

sample_dt1 = sample_dt['pyt_hist'].str.split(',', expand=True).astype(float)
sample_dt = sample_dt.assign(max_value=sample_dt1.max(axis=1))

示例数据:

index    pyt_hist
0 0,0,0,0,0,0,0,0,0,0,0
1 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
2 0,0,0,360,420,392,361,330,300,269,239,208,177
3 0,0,0,0,0,0,0,0,0,0,0,0,0,0,23,0,23,0,0,56,0

预期结果:

index    pyt_hist                                           max_value
0 0,0,0,0,0,0,0,0,0,0,0 0
1 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 0
2 0,0,0,360,420,392,361,330,300,269,239,208,177 420
3 0,0,0,0,0,0,0,0,0,0,0,0,0,0,23,0,23,0,0,56,0 0

使用我的代码获得的结果:

index    pyt_hist                                           max_value
0 0,0,0,0,0,0,0,0,0,0,0 0.0
1 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 0.0
2 0,0,0,360,420,392,361,330,300,269,239,208,177 420.0
3 0,0,0,0,0,0,0,0,0,0,0,0,0,0,23,0,23,0,0,56,0 56.0

最佳答案

您非常接近,sample_dt1.iloc[:,:13] 为您提供了 sample_dt1 的前 13 列。所以你可以这样做:

sample_dt = sample_dt.assign(max_value=sample_dt1.iloc[:,:13].max(axis=1))

关于python - 从列中的字符串中提取每行的最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57165219/

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