Python 迭代列中的字典列表并创建新列

Python 迭代列中的字典列表并创建新列,python,pandas,dataframe,dictionary,Python,Pandas,Dataframe,Dictionary,我想从pandas dataframe列解析json字典,迭代dict并将它们分配给新的列值 这是一列数据帧:df['column'][0] [{'Name': 'Vacant', 'Value': 3904000, 'Unit': 'Qty'}, {'Name': 'Vacant', 'Value': 11.7, 'Unit': 'Pct'}, {'Name': 'Absorption', 'Value': 415000, 'Unit': 'Units'}, {'Name': 'Abso

我想从pandas dataframe列解析json字典,迭代dict并将它们分配给新的列值

这是一列数据帧:
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'}]
我有以下代码来迭代pandas dataframe中的每一行,然后迭代列表中的每一个DICT并创建新列

# 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)
                  
预期输出,新列:


您需要处理数据帧的“列”列。 我想你应该用apply。此外,此更改将在dataframe中进行,因此您的函数可能会被修改

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)

是的,我在寻找解析逻辑<代码>应用有意义。