拆分列以修改数据框

时间:2017-07-27 17:08:46

标签: python pandas jupyter-notebook

我有一个数据框df,其中包含名为"属性"的列,如下所示

attributes
{"id":1,"firstname":"Joe","lastname":"Lee"}
{"id":12,"firstname":"Brian","lastname":"Li"}
{"id":2,"firstname":"Ron","lastname":"Stein"}

如何将此列拆分为不同的列,以便我的数据框现在看起来像 -

attributes                                     id    firstname  lastname
{"id":1,"firstname":"Joe","lastname":"Lee"}    1     Joe         Lee
{"id":12,"firstname":"Brian","lastname":"Li"}  12    Brian       Li
{"id":2,"firstname":"Ron","lastname":"Stein"}  2     Ron         Stein

我正在尝试访问每个值,如

df.attributes.id
df.attributes.firstname

但我无法做到!我们将不胜感激。

3 个答案:

答案 0 :(得分:4)

您可以使用apply

In [98]: df.attributes.apply(pd.Series)
Out[98]:
  firstname  id lastname
0       Joe   1      Lee
1     Brian  12       Li
2       Ron   2    Stein

将结果加入原始df

In [99]: df.join(df.attributes.apply(pd.Series))
Out[99]:
                                          attributes firstname  id lastname
0  {u'lastname': u'Lee', u'id': 1, u'firstname': ...       Joe   1      Lee
1  {u'lastname': u'Li', u'id': 12, u'firstname': ...     Brian  12       Li
2  {u'lastname': u'Stein', u'id': 2, u'firstname'...       Ron   2    Stein

In [100]: dff = df.join(df.attributes.apply(pd.Series))

访问firstname

In [101]: dff.firstname
Out[101]:
0      Joe
1    Brian
2      Ron
Name: firstname, dtype: object

答案 1 :(得分:2)

我们可以使用pd.DataFrame.from_records()

演示:

In [24]: df
Out[24]:
                                          attributes       aaa
0   {'id': 1, 'firstname': 'Joe', 'lastname': 'Lee'}  0.345719
1  {'id': 12, 'firstname': 'Brian', 'lastname': '...  0.849491
2  {'id': 2, 'firstname': 'Ron', 'lastname': 'Ste...  0.412470

In [25]: df.join(pd.DataFrame.from_records(df.attributes.values, index=df.index))
Out[25]:
                                          attributes       aaa firstname  id lastname
0   {'id': 1, 'firstname': 'Joe', 'lastname': 'Lee'}  0.345719       Joe   1      Lee
1  {'id': 12, 'firstname': 'Brian', 'lastname': '...  0.849491     Brian  12       Li
2  {'id': 2, 'firstname': 'Ron', 'lastname': 'Ste...  0.412470       Ron   2    Stein

答案 2 :(得分:0)

普通pd.DataFrame构造函数将与列表

一起使用
pd.DataFrame(df.attributes.tolist())

  firstname  id lastname
0       Joe   1      Lee
1     Brian  12       Li
2       Ron   2    Stein

使用join

df.join(pd.DataFrame(df.attributes.tolist(), df.index))

                                                attributes firstname  id lastname
0    {u'lastname': u'Lee', u'id': 1, u'firstname': u'Joe'}       Joe   1      Lee
1  {u'lastname': u'Li', u'id': 12, u'firstname': u'Brian'}     Brian  12       Li
2  {u'lastname': u'Stein', u'id': 2, u'firstname': u'Ron'}       Ron   2    Stein