gpt4 book ai didi

python - Pandas :从分层数据创建字典

转载 作者:太空宇宙 更新时间:2023-11-03 15:45:13 24 4
gpt4 key购买 nike

假设我有以下数据框df:

      A            B       
0 mother1 NaN
1 NaN child1
2 NaN child2
3 mother2 NaN
4 NaN child1
5 mother3 NaN
6 NaN child1
7 NaN child2
8 NaN child3

你怎么能把它变成一个产生的字典:

results={'mother1':['child1','child2'],'mother2':['child1'],'mother3':['child1','child2','child3'] }

我的看法:

import pandas as pd
import numpy as np

results={}

for index1,row1 in df.iterrows():
if row1['A'] is not np.nan:
children=[]
for index2,row2 in df.iterrows():
if row2['B'] is not np.nan:
children.append(row2['B'])
results[row1['A']]=children

但是,结果是错误的:

In[1]: results
Out[1]:
{'mother1': ['child1', 'child2', 'child1', 'child1', 'child2', 'child3'],
'mother2': ['child1', 'child2', 'child1', 'child1', 'child2', 'child3'],
'mother3': ['child1', 'child2', 'child1', 'child1', 'child2', 'child3']}

最佳答案

这是一种方法:

df['A'].fillna(method='ffill', inplace=True)

给予:

         A       B
0 mother1 NaN
1 mother1 child1
2 mother1 child2
3 mother2 NaN
4 mother2 child1
5 mother3 NaN
6 mother3 child1
7 mother3 child2
8 mother3 child3

然后删除子 NA:

df.dropna(subset=['B'], inplace=True)

给予:

         A       B
1 mother1 child1
2 mother1 child2
4 mother2 child1
6 mother3 child1
7 mother3 child2
8 mother3 child3

然后您可以使用 groupby 和字典理解来获得最终结果:

results = {k: v['B'].tolist() for k, v in df.groupby('A')}

结果:

{'mother1': ['child1', 'child2'],
'mother2': ['child1'],
'mother3': ['child1', 'child2', 'child3']}

关于python - Pandas :从分层数据创建字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50327311/

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