简体   繁体   English

熊猫:从分层数据创建字典

[英]Pandas: create dictionary from hierarchical data

Say I have the following dataframe df : 假设我有以下数据帧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

How could you turn this into a dictionary that yields: 你怎么能把它变成一个字典,产生:

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

My take on it: 我接受它:

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

However, the result is wrong: 但是,结果是错误的:

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']}

Here's one way: 这是一种方式:

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

Giving: 赠送:

         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

Then drop the child NAs: 然后放下孩子NAs:

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

Giving: 赠送:

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

You can then use groupby and a dictionary comprehension to get you to the final result: 然后,您可以使用groupby和字典理解来获得最终结果:

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

Results: 结果:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM