简体   繁体   English

如何使用 Pandas 将行中的多个字典拆分为新行

[英]How to split multiple dictionaries in row into new rows using Pandas

I have the following dataframe with multiple dictionaries in a list in the Rules column.我在规则列的列表中有以下 dataframe 和多个词典。

SetID      SetName             Rules
    0         Standard_1        [{'RulesID': '10', 'RuleName': 'name_abc'}, {'RulesID': '11', 'RuleName': 'name_xyz'}]   
    1         Standard_2        [{'RulesID': '12', 'RuleName': 'name_arg'}]

The desired output is:所需的 output 是:

SetID      SetName             RulesID        RuleName         
    0         Standard_1        10            name_abc
    0         Standard_1        11            name_xyz 
    1         Standard_2        12            name_arg

It might be possible that there are more than two dictionaries inside of the list.列表中可能有两个以上的词典。

I am thinking about a pop, explode or pivot function to build the dataframe but I have no clue how to start.我正在考虑使用 pop、explode 或 pivot function 来构建 dataframe,但我不知道如何开始。

Each advice will be very appreciated!每个建议将不胜感激!

EDIT: To build the dataframe you can use the follwing dataframe constructor:编辑:要构建 dataframe,您可以使用以下 dataframe 构造函数:

# initialize list of lists
data = [[0, 'Standard_1', [{'RulesID': '10', 'RuleName': 'name_abc'}, {'RulesID': '11', 'RuleName': 'name_xyz'}]], [1, 'Standard_2', [{'RulesID': '12', 'RuleName': 'name_arg'}]]]
 
# Create the pandas DataFrame
df = pd.DataFrame(data, columns = ['SetID', 'SetName', 'Rules'])

You can use explode :您可以使用explode

tmp = df.explode('Rules').reset_index(drop=True)
df = pd.concat([tmp, pd.json_normalize(tmp['Rules'])], axis=1).drop('Rules', axis=1)

Output: Output:

>>> df
   SetID     SetName RulesID  RuleName
0      0  Standard_1      10  name_abc
1      0  Standard_1      11  name_xyz
2      1  Standard_2      12  name_arg

One-liner version of the above:上面的单行版本:

df.explode('Rules').reset_index(drop=True).pipe(lambda x: pd.concat([tmp, pd.json_normalize(tmp['Rules'])], axis=1)).drop('Rules', axis=1)

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

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