简体   繁体   English

遍历 pandas 列中的字典列表并创建新列

[英]Iterate over a list of dictionaries in pandas column and create new columns

I'd like to parse json dictionaries from a pandas dataframe column, iterate over the dicts and assign them to new column values.我想从 Pandas 数据框列解析 json 字典,遍历字典并将它们分配给新的列值。

Here's a column of dataframe: df['Column'][0]这是一列数据框: df['Column'][0]

[{'Name': 'Vacant', 'Value': 3904000, 'Unit': 'Qty'},
 {'Name': 'Vacant', 'Value': 11.7, 'Unit': 'Pct'},
 {'Name': 'Absorption', 'Value': 415000, 'Unit': 'Units'},
 {'Name': 'AbsorpOcc', 'Value': 1.4, 'Unit': 'Pct'},
 {'Name': 'Occupied', 'Value': None, 'Unit': 'Qty'}]

I have the following code to iterate over each row in pandas dataframe, and then iterate over each dicts in a list and create new columns.我有以下代码来遍历 Pandas 数据帧中的每一行,然后遍历列表中的每个字典并创建新列。

# Iterate over dataframe to parse select rows   
# Declare array
s = ""

#Iterate over each row in Dataframe
for index, row in df.iterrows():
    
    # Iterate over each json object in each row in DataFrame
    for i in range(0,len(row['Column'])):
        
        for k,v in row['Column'][i].items():
            
            # Concat string labels to assign them as column names
            if type(v) == str:
            
                s += v
            
        print(s)
                  

Expected Output, new columns:预期输出,新列:

在此处输入图片说明

You have a specific requirement to process the 'Column' column of the dataframe.您有处理数据框的“列”列的特定要求。 I think you should use apply https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.apply.html .我认为您应该使用 apply https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.apply.html Also this change would be in place in dataframe so your function could be.此外,此更改将在数据框中进行,因此您的功能可以。

def func(row):
    # your parsing logic
    index = row.name
    # {'Name': 'Vacant', 'Value': 3904000, 'Unit': 'Qty'}
    # col = 'Vacant', value = 3904000
    df.loc[index, col] = value
df.apply(func, axis=1)

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

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