当 json_normalize 无法遍历列以展平时如何修复它?

Rus*_*ord 4 python json pandas

我有一个看起来像这样的数据框:

ID       phone_numbers
1        [{u'updated_at': u'2017-12-02 15:29:54', u'created_at': u'2017-12-0 
          2 15:29:54', u'sms': 0, u'number': u'1112223333', u'consumer_id': 
          12345, u'organization_id': 1, u'active': 1, u'deleted_at': 
           None, u'type': u'default', u'id': 1234}]
Run Code Online (Sandbox Code Playgroud)

我想获取 phone_numbers 列并将其中的信息展平,以便我可以查询“id”字段。

当我尝试时;

json_normalize(df.phone_numbers)
Run Code Online (Sandbox Code Playgroud)

我得到错误:

AttributeError: 'str' 对象没有属性 'itervalues'

我不确定为什么会产生这个错误以及为什么我不能展平这个列。

编辑:

最初是从响应对象(r.text)中读取的 JSON 字符串:

https://docs.google.com/document/d/1Iq4PMcGXWx6O48sWqqYnZjG6UMSZoXfmN1WadQLkWYM/edit?usp=sharing

编辑:

通过此命令将我需要展平的列转换为 JSON

a = df.phone_numbers.to_json()

{"0":[{"updated_at":"2018-04-12 12:24:04","created_at":"2018-04-12 12:24:04","sms":0,"number":"","consumer_id":123,"org_id":123,"active":1,"deleted_at":null,"type":"default","id":123}]}
Run Code Online (Sandbox Code Playgroud)

jez*_*ael 6

使用列表理解与展平ID并向字典添加新元素:

df = pd.DataFrame({'ID': [1, 2], 'phone_numbers': [[{'a': '2017', 'b': '2017', 'sms': 1}, 
                                                    {'a': '2018', 'b': '2017', 'sms': 2}], 
                                                  [{'a': '2017', 'b': '2017', 'sms': 3}]]})
print (df)
   ID                                      phone_numbers
0   1  [{'a': '2017', 'b': '2017', 'sms': 1}, {'a': '...
1   2             [{'a': '2017', 'b': '2017', 'sms': 3}]

df = pd.DataFrame([dict(y, ID=i) for i, x in df.values.tolist() for y in x])
print (df)  

   ID     a     b  sms
0   1  2017  2017    1
1   1  2018  2017    2
2   2  2017  2017    3
Run Code Online (Sandbox Code Playgroud)

编辑:

df = pd.DataFrame({'phone_numbers':{"0":[{"type":"default","id":123}]}})

df = pd.DataFrame([y for x in df['phone_numbers'].values.tolist() for y in x])
print (df) 
    id     type
0  123  default
Run Code Online (Sandbox Code Playgroud)


alv*_*tes 5

我不确定,但我认为 json normalize expect 作为第一个参数 a json 而不是 a pd.series,首先将系列转换为 dict 或 dict 列表。你可以用to_dict()

json_normalize(df.phone_numbers.to_dict())
Run Code Online (Sandbox Code Playgroud)

  • 当我尝试此操作时,它将整个列转换为一行。 (4认同)