首页 > 解决方案 > 从列中的字符串中提取每行的最大值

问题描述

我在 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

标签: pythonpython-3.xpandasnumpy

解决方案


你非常接近, sample_dt1.iloc[:,:13]给你前 13 列sample_dt1。所以你可以这样做:

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

推荐阅读