python - Pandas groupby agg 用列标题的副本填满表格
问题描述
我有一个包含溢出多行的文本的 DataFrame,我想将具有相同索引的行集合并在一起。大多数时候它都有效。但是在某些情况下,它不是合并列,而是简单地将列标题的副本涂抹在整个 DataFrame 中。
我正在使用以下代码:
table = table.groupby(chosen_column).agg(lambda x: ' '.join(
x.fillna('').drop_duplicates()))
前表(经过一些处理):
index no_header_2 no_header_3 no_header_4 no_header_3 unit_price no_header_3
0 2.0 Production Supplies 22 MAY 2019 4 text 35.66 142.64
1 2.0 Production Supplies 22 MAY 2019 4 each 35.66 142.64
2 4.0 Supplier Product Number 123456 22 MAY 2019 6 each 13.24 79.44
3 4.0 Production Supplies 22 MAY 2019 6 each 13.24 79.44
4 6.0 Supplier Product Number 234567 22 MAY 2019 3 each 21.35 64.05
5 6.0 Production Supplies 22 MAY 2019 3 each 21.35 64.05
6 8.0 Supplier Product Number 345678 22 MAY 2019 10 each 6.03 60.30
7 8.0 Production Supplies 22 MAY 2019 10 each 6.03 60.30
之后的表格(打印时切断)我也不清楚为什么将索引添加为 2 级标题并在下面打印在它自己的行上。
index ... no_header_3
unit_price ...
13.24 index no_header_2 no_header_3 no_header_4 no_h... ... index no_header_2 no_header_3 no_header_4 no_h...
21.35 index no_header_2 no_header_3 no_header_4 no_h... ... index no_header_2 no_header_3 no_header_4 no_h...
35.66 index no_header_2 no_header_3 no_header_4 no_h... ... index no_header_2 no_header_3 no_header_4 no_h...
6.03 index no_header_2 no_header_3 no_header_4 no_h... ... index no_header_2 no_header_3 no_header_4 no_h...
任何意见,将不胜感激。我试图弄清楚像这样将标题复制到每个单元格中的内容,并且试图找出正确的方法来做到这一点。目标是共享相同索引的所有文本将一起附加到一个单元格中,并删除重复项。
解决方案
您可以将数据框转换为python 列表
DF.values.tolist()
然后你可以string.join()
在每个列表上使用。这将永远为你工作。
推荐阅读
- python - 使用 numpy 时显示“列表超出范围”
- spring-boot - 为什么我的网络服务在使用 .hasIpAddress() 时禁止我的连接?
- javascript - 如何在不重新加载页面的情况下提交数据?
- mysql - 如何在不给出任何键的情况下选择表中所有重复的数据?
- python - 按钮功能不起作用(不更改动画演示中的幻灯片)
- r - 将列名分配给df,阻止字符自动转换为数字?
- r - 如何分组一个data.frame每个整数行?
- javascript - 无法读取未定义的属性“swaggerUi”
- mysql - MySQL:使用联合将多个选择查询拆分到各自的表中
- html - 在播放预加载视频