Expand pandas dataframe column of dict into dataframe columns [duplicate]

时间秒杀一切 提交于 2019-12-11 08:57:33

问题


I have a Pandas DataFrame where one column is a Series of dicts, like this:

   colA  colB                                  colC
0     7     7  {'foo': 185, 'bar': 182, 'baz': 148}
1     2     8  {'foo': 117, 'bar': 103, 'baz': 155}
2     5    10  {'foo': 165, 'bar': 184, 'baz': 170}
3     3     2  {'foo': 121, 'bar': 151, 'baz': 187}
4     5     5  {'foo': 137, 'bar': 199, 'baz': 108}

I want the foo, bar and baz key-value pairs from the dicts to be columns in my dataframe, such that I end up with this:

   colA  colB  foo  bar  baz
0     7     7  185  182  148
1     2     8  117  103  155
2     5    10  165  184  170
3     3     2  121  151  187
4     5     5  137  199  108

How do I do that?


回答1:


TL;DR

df = df.drop('colC', axis=1).join(pd.DataFrame(df.colC.values.tolist()))

Elaborate answer

We start by defining the DataFrame to work with, as well as a importing Pandas:

import pandas as pd


df = pd.DataFrame({'colA': {0: 7, 1: 2, 2: 5, 3: 3, 4: 5},
                   'colB': {0: 7, 1: 8, 2: 10, 3: 2, 4: 5},
                   'colC': {0: {'foo': 185, 'bar': 182, 'baz': 148},
                    1: {'foo': 117, 'bar': 103, 'baz': 155},
                    2: {'foo': 165, 'bar': 184, 'baz': 170},
                    3: {'foo': 121, 'bar': 151, 'baz': 187},
                    4: {'foo': 137, 'bar': 199, 'baz': 108}}})

The column colC is a pd.Series of dicts, and we can turn it into a pd.DataFrame by turning each dict into a pd.Series:

pd.DataFrame(df.colC.values.tolist())
# df.colC.apply(pd.Series). # this also works, but it is slow

which gives the pd.DataFrame:

   foo  bar  baz
0  154  190  171
1  152  130  164
2  165  125  109
3  153  128  174
4  135  157  188

So all we need to do is:

  1. Turn colC into a pd.DataFrame
  2. Delete the original colC from df
  3. Join the convert colC with df

That can be done in a one-liner:

df = df.drop('colC', axis=1).join(pd.DataFrame(df.colC.values.tolist()))

With the contents of df now being the pd.DataFrame:

   colA  colB  foo  bar  baz
0     2     4  154  190  171
1     4    10  152  130  164
2     4    10  165  125  109
3     3     8  153  128  174
4    10     9  135  157  188


来源:https://stackoverflow.com/questions/54344114/expand-pandas-dataframe-column-of-dict-into-dataframe-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!