首页 > 解决方案 > 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...

任何意见,将不胜感激。我试图弄清楚像这样将标题复制到每个单元格中的内容,并且试图找出正确的方法来做到这一点。目标是共享相同索引的所有文本将一起附加到一个单元格中,并删​​除重复项。

标签: pythonpandaspandas-groupby

解决方案


您可以将数据框转换为python 列表

DF.values.tolist() 

然后你可以string.join()在每个列表上使用。这将永远为你工作。


推荐阅读