【问题标题】:How to group by dataframe's cells which contain lists in Python?如何按包含 Python 列表的数据框单元格进行分组?
【发布时间】:2019-11-15 23:12:08
【问题描述】:

我正在使用 Python 和 Pandas,试图以一种有效的方式根据 ID 列表而不是唯一 ID 来总结不同行中数据框的值。

df:

Name  -  ID  - Related IDs          - Value
z     -  123 - ['aaa','bbb','ccc']  -  10
w     -  456 - ['aaa']              -  20
y     -  789 - ['ggg','hhh','jjj']  -  50
x     -  012 - ['jjj','hhh']        -  60
r     -  015 - ['hhh']              -  15

可以尝试按列表的元素分解每一行,但它可能会重复要求和的值,并且在时间和资源方面可能不是一个有效的解决方案。

```python
f = {'Sum': 'sum'}

df = df.groupby(['Related IDs']).agg(f) 
#it is not working has is matching element wise 
#rather then by element

df = df.reset_index()
```

我期待的是一个新列“Sum”,它汇总了具有一个或多个共同相关 ID 的行的值“Value”。如下:

Name  -  ID  - Related IDs          - Value - Sum
z     -  123 - ['aaa','bbb','ccc']  -  10  -  30
w     -  456 - ['aaa']              -  20  -  30
y     -  789 - ['ggg','hhh','jjj']  -  50  -  125
x     -  012 - ['jjj','hhh']        -  60  -  125
r     -  015 - ['hhh']              -  15  -  125

【问题讨论】:

  • df['Sum'] = df.groupby(['Related IDs'])['Value'].transform('sum')
  • 尝试下面的方法我得到了一个不可散列的错误类型...df = pd.DataFrame({'Related IDs': [['aaa','bbb','ccc'], ['aaa'], ['ddd','eee','fff']], 'Val': [400, 100, 60]}) print(df) df['Sum'] = df.groupby(['Related IDs'])['Val'].transform('sum')
  • @jezrael dup 问题不能解决这个问题。 list 不可散列,因此不能为 groupby。
  • @QuangHoang - 我们去回答吧;)谢谢你的评论。
  • 标签编码列表怎么样?还是把它们变成字符串?

标签: python pandas list pandas-groupby


【解决方案1】:

将networkx 与connected_components 一起使用:

import networkx as nx
from itertools import combinations, chain

#if necessary convert to lists 
df['Related IDs'] = df['Related IDs'].apply(ast.literal_eval)

#create edges (can only connect two nodes)
L2_nested = [list(combinations(l,2)) for l in df['Related IDs']]
L2 = list(chain.from_iterable(L2_nested))
print (L2)
[('aaa', 'bbb'), ('aaa', 'ccc'), ('bbb', 'ccc'), 
 ('ggg', 'hhh'), ('ggg', 'jjj'), ('hhh', 'jjj'), ('jjj', 'hhh')]

#create the graph from the dataframe
G=nx.Graph()
G.add_edges_from(L2)
connected_comp = nx.connected_components(G)

#create dict for common values
node2id = {x: cid for cid, c in enumerate(connected_comp) for x in c}

#create groups by mapping first value of column Related IDs
groups = [node2id.get(x[0]) for x in df['Related IDs']]
print (groups)
[0, 0, 1, 1, 1]

#get sum to new column
df['Sum'] = df.groupby(groups)['Value'].transform('sum')
print (df)
  Name   ID      Related IDs  Value  Sum
0    z  123  [aaa, bbb, ccc]     10   30
1    w  456            [aaa]     20   30
2    y  789  [ggg, hhh, jjj]     50  125
3    x   12       [jjj, hhh]     60  125
4    r   15            [hhh]     15  125

【讨论】:

    猜你喜欢
    • 2018-09-24
    • 2017-06-07
    • 2021-12-21
    • 2017-05-24
    • 2020-01-18
    • 2020-11-05
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    相关资源
    最近更新 更多