在python

时间:2017-04-03 05:39:01

标签: python split substring python-3.5

我有一个数据框,如下所示:

 varA                   json_data
 'string1'      {"str":{"str":"string","str":12345,"str":"str","str":"str","xyz":"1234","zyx":"string1","str":[{"str":"str","str":"str"}],"str":["str","str"],"str":["str"]},"str":"str"}

 'string2'      {"str":{"str":"string","str":12345,"str":"str","str":"str","xyz":"4567","zyx":"string2","str":[{"str":"str","str":"str"}],"str":["str","str"],"str":["str"]},"str":"str"}

 'string3'      {"str":{"str":"string","str":12345,"str":"str","str":"str","xyz":"8910","zyx":"string3","str":[{"str":"str","str":"str"}],"str":["str","str"],"str":["str"]},"str":"str"}

我需要创建新列,并将从json_data中提取的数字填充到第一个," string1"," string2"和" string3"值到第二个新列。结果数据集如下所示:

   varA        json_data    xyz   zyx
 'string'        ...       1234  string1
 'string'        ...       4567  string2
 'string'        ...       8910  string3

我的代码如下:

df['xyz'] = df['json_data'].str.split('xyz":')[1].split('","zyx')[0]

然而,我收到一个错误:

AttributeError: 'list' object has no attribute 'split'

我该如何解决?还有其他选择吗?

0 个答案:

没有答案