python - 从列中的字符串中提取每行的最大值
问题描述
我在 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]
给你前 13 列sample_dt1
。所以你可以这样做:
sample_dt = sample_dt.assign(max_value=sample_dt1.iloc[:,:13].max(axis=1))
推荐阅读
- pyomo - 为什么 CBC 找不到 Pyomo 热启动文件?
- javascript - 如何使用 TypeScript 使用自定义查询参数扩展默认 NextJS 路由器
- angular - 使用 ng2-pdf-viewer 加载本地 PDF
- java - quarkus 微服务中的性能问题
- python - 您正在尝试添加一个不可为空的字段“名称”以在没有默认值的情况下进行评论
- c++ - C++ 输出的数据重叠?
- c# - 恢复锁定模式失败,出现错误 NU1004:包锁定文件不一致
- javascript - 正则表达式检查不同的顺序?
- javascript - 使背景适合页面网格,没有滚动条
- python - 创建python包后无法调用函数