拆分python中不同列中列内的字典列表

问题描述 投票:1回答:3

我有这样的数据帧

data = {'col_1': [1, 2],
        'col_2': [[{'KEY': 'A', 'VALUE': 'a'}], [{'KEY': 'B', 'VALUE': 'b'}]],
        'col_3': [[{'KEY': 'C', 'VALUE': 'c'}], [{'KEY': 'A', 'VALUE': 'a'}]]}
pd.DataFrame.from_dict(data)

    col_1   col_2                           col_3
0   1       [{'KEY': 'A', 'VALUE': 'a'}]    [{'KEY': 'C', 'VALUE': 'c'}]
1   2       [{'KEY': 'B', 'VALUE': 'b'}]    [{'KEY': 'A', 'VALUE': 'a'}]

我想转换每列中的字典列表,这样我得到以下输出

    col_1   col_2_KEY   col_2_VALUE     col_3_KEY   col_3_VALUE
0   1       A           a               C           c
1   2       B           b               A           a

EDIT1

可能存在列值为null的情况

data = {'col_1': [1, 2],
        'col_2': [[{'KEY': 'A', 'VALUE': 'a'}], [{'KEY': 'B', 'VALUE': 'b'}]],
        'col_3': [[{'KEY': 'C', 'VALUE': 'c'}], [{'KEY': 'A', 'VALUE': 'a'}]]}
pd.DataFrame.from_dict(data)

    col_1   col_2                           col_3
0   1       [{'KEY': 'A', 'VALUE': 'a'}]    []
1   2       [{'KEY': 'B', 'VALUE': 'b'}]    [{'KEY': 'A', 'VALUE': 'a'}]

预期产出

    col_1   col_2_KEY   col_2_VALUE     col_3_KEY   col_3_VALUE
0   1       A           a               <blank>     <blank> 
1   2       B           b               A           a
python python-3.x pandas dictionary
3个回答
1
投票

你可以试试:

df = pd.concat([df.drop(['col_2','col_3'], axis=1)
                , df['col_2'].apply(lambda x:pd.Series(x[0] if len(x)>0 else {})).rename(columns={'KEY':'col_2_KEY','VALUE':'col_2_VALUE'})
                , df['col_3'].apply(lambda x:pd.Series(x[0] if len(x)>0 else {})).rename(columns={'KEY':'col_3_KEY','VALUE':'col_3_VALUE'})
                ], axis=1)
print(df)

   col_1 col_2_KEY col_2_VALUE col_3_KEY col_3_VALUE
0      1         A           a         C           c
1      2         B           b         A           a

1
投票

你可以用

def splitter(item):
    try:
        d = item[0]
        return (d["KEY"], d["VALUE"])
    except IndexError:
        return (None, None)


for i in [2, 3]:
    df["col_{}_KEY".format(i)], df["col_{}_VALUE".format(i)] = df["col_{}".format(i)].apply(splitter)
    df.drop("col_{}".format(i), axis=1, inplace=True)

生产

   col_1 col_2_KEY col_2_VALUE col_3_KEY col_3_VALUE
0      1         A           B         C           A
1      2         a           b         c           a

1
投票

使用list comprehension获取字典值,使用drop获取cols:

cols = ['col_2','col_3']
for col in cols:
    df[col+'_KEY'] = [d[0].get('KEY') for d in df[col]]
    df[col+'_VALUE'] = [d[0].get('VALUE') for d in df[col]]

df.drop(cols, axis=1, inplace=True)

print(df)
   col_1 col_2_KEY col_2_VALUE col_3_KEY col_3_VALUE
0      1         A           a         C           c
1      2         B           b         A           a

更新:

cols = ['col_2','col_3']
for col in cols:
    df[col+'_KEY'] = [d[0].get('KEY') if d else '' for d in df[col] ]
    df[col+'_VALUE'] = [d[0].get('VALUE') if d else '' for d in df[col]]

df.drop(cols, axis=1, inplace=True)

print(df)
   col_1 col_2_KEY col_2_VALUE col_3_KEY col_3_VALUE
0      1         A           a                      
1      2         B           b         A           a
© www.soinside.com 2019 - 2024. All rights reserved.