将列表单独列入列和值

时间:2018-05-28 09:03:36

标签: python pandas

我的数据框包含一个值为“[{"key":"title","value":"activation"},{"key":"remarks","value":"activation"}]

的”ExtData“列

我必须将这些数据分开并创建一个带有“title”和“remarks”列名的新数据框,其值为“activation”,即“key”为列名,其“value”为值。

我有这样的数据框

partner               ExtData
xyz          [{"key":"title","value":"activation"}, {"key":"remarks","value":"activation"}]
abc          [{"key":"title","value":"activation"}, {"key":"remarks","value":"activation"}]

我需要输出

作为新数据框
**partner**   **title**      **remarks**
xyz           activation     activation
abc           activation     activation

使用pandas和python。

2 个答案:

答案 0 :(得分:1)

以下是使用DataFrame.apply方法的解决方案:

def separate_extdata(row):
    for d in row['ExtData']:
            row[d['key']] = d['value']
    return row.drop('ExtData')

df = pd.DataFrame(
    [
      ('xyz', [{"key": "title", "value": "activation"},
               {"key":"remarks","value":"activation"}]), 
      ('abc', [{"key":"title","value":"activation"}, 
               {"key":"remarks","value":"activation"}])], 
    columns=['partner', 'ExtData']
)
df.apply(separate_extdata, axis=1)
#   partner       title     remarks
# 0     xyz  activation  activation
# 1     abc  activation  activation

答案 1 :(得分:0)

new_df = pandas.Dataframe()
new_index = 0
for i, row in df.iterrows():
    if 'key' in row['ExtData']:
       k = row['ExtData']['key']
       v = row['ExtData']['value']
       new_df.loc[new_index, k] = v
       new_index+=1