[英]Split a column to modify dataframe
I have a dataframe df with a column called "attributes", shown below 我有一个数据框df,其列名为“属性”,如下所示
attributes
{"id":1,"firstname":"Joe","lastname":"Lee"}
{"id":12,"firstname":"Brian","lastname":"Li"}
{"id":2,"firstname":"Ron","lastname":"Stein"}
How do I split this column into different columns so that my dataframe now looks like - 如何将此列拆分为不同的列,以便数据框现在看起来像-
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
I am trying to access each value like 我正在尝试访问每个值
df.attributes.id
df.attributes.firstname
But I am unable to! 但是我做不到! Your help will be appreciated.
您的帮助将不胜感激。
You could use apply
您可以使用
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
join the result to original df
将结果加入原始
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))
Access firstname
访问
firstname
In [101]: dff.firstname
Out[101]:
0 Joe
1 Brian
2 Ron
Name: firstname, dtype: object
we can use pd.DataFrame.from_records()
: 我们可以使用
pd.DataFrame.from_records()
:
Demo: 演示:
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
Normal pd.DataFrame
constructor will work with list 正常的
pd.DataFrame
构造函数将与列表一起使用
pd.DataFrame(df.attributes.tolist())
firstname id lastname
0 Joe 1 Lee
1 Brian 12 Li
2 Ron 2 Stein
With join
与
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.