gpt4 book ai didi

python - Pandas 按值分组并合并行

转载 作者:太空宇宙 更新时间:2023-11-03 11:14:04 27 4
gpt4 key购买 nike

我有一个 DataFrame,我想合并包含相同值的行

toy = [
[10, 11],
[21, 22],
[11, 15],
[22, 23],
[15, 33]
]

toy = pd.DataFrame(toy, columns = ['ID1', 'ID2'])
    ID1 ID2
0 10 11
1 21 22
2 11 15
3 22 23
4 15 33

我希望得到的是

    0   1   2   3
0 10 11 15 33.0
1 21 22 23 NaN

因此合并包含任何相同值的行。

我的解决方案非常优雅,我正在寻找正确的方法来做到这一点...递归?通过...分组?嗯..

#### Feel Free to NOT read this... ###
for k in range(100):
print(k)

merge_df = []
merged_indices = []
for i, row in toy.iterrows():
if i in merged_indices:
continue
cp = toy.copy()
merge_rows = cp[cp.isin(row.values)].dropna(how="all")
merged_indices = merged_indices + list(merge_rows.index)
merge_rows = np.array(toy.iloc[merge_rows.index]).flatten()
merge_rows = np.unique(merge_rows)
merge_df.append(merge_rows)

if toy.shape[0] == len(merge_df):
break
toy = pd.DataFrame(merge_df).copy()

最佳答案

听起来像是网络问题,所以我使用 networkx

import networkx as nx 
G=nx.from_pandas_edgelist(toy, 'ID1', 'ID2')
l=list(nx.connected_components(G))
newdf=pd.DataFrame(l)
newdf
Out[896]:
0 1 2 3
0 33 10 11 15.0
1 21 22 23 NaN

关于python - Pandas 按值分组并合并行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55067317/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com