使用to_json()的pandas DataFrame到dicts列表

时间:2018-05-11 19:03:29

标签: python json string pandas datetime

所以,我正在用pandas读取xlsx文件,然后解析日期时间(excel' s float)

然后我需要将它解析为Json,并且我遇到了一些问题。

第1步(在使用to_json()解析之前)

df = pandas.read_excel('test.xlsx', names=['date', 'value', 'source'])
df['date'] = pandas.to_datetime(df['date'], format='%b %d %Y.%f')
print(df)

回报是

        date  value                            source
0 2012-05-22      1              xxxxxxxxxxxxxxxxxxxx
1 2012-05-25      1                     xxxxxxxxxxxxx
2 2012-05-30      1  xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
3 2012-06-01      1                        xxxxxxxxxx
4 2012-06-08      1                 xxxxxxxxxxxxxxxxx

一切似乎都很好,然后我得到了o to_json

payload = df.to_json()

回报是

{"date":{"0":1337644800000,"1":1337904000000,"2":1338336000000,"3":1338508800000,"4":1339113600000},"value":{"0":1,"1":1,"2":1,"3":1,"4":1},"source":{"0":"xxxxxxxxxxxxxxx","1":"xxxxxxxxxx","2":"xxxxxxxxxxx","3":"xxxxxxxxxxxxxxxx","4":"xxxxxxxxxxxxxxx"}}

那么我做错了什么?我在to_json()上错过了args吗? Halp请:c

我需要它像这样:

[{"date":"2012-05-22","value":1,"source":"xxxxxxxxxxxxxxxxxxxx"},
{"date":"2012-05-25","value":1,"source":"xxxxxxxxxxxxxxxxxxxx"},
{"date":"2012-05-30","value":1,"source":"xxxxxxxxxxxxxxxxxxxxx"},
{"date":"2012-06-01","value":1,"source":"xxxxxxxxxxxxxxxxxxxx"},
{"date":"2012-06-08","value":1,"source":"xxxxxxxxxxxxxxxxxxxxxx"}]

2 个答案:

答案 0 :(得分:2)

您需要一些修复 -

  1. 将您的日期列转换为字符串,因为当前正在将您的日期时间列强制转换为Unix整数时间戳。或者,将date_format参数与to_json一起使用,如另一个答案所示。
  2. 保存到json时更改方向;指定orient='records'
  3. df['date'] = pandas.to_datetime(df['date'], format='%b %d %Y.%f').astype(str)
    payload = df.to_json(orient='records')
    

    print(payload)
    '[{"date":"2012-05-22","source":"xxxxxxxxxxxxxxxxxxxx","value":1},{"date":"2012-05-25","source":"xxxxxxxxxxxxxxxxxxxx","value":1},{"date":"2012-05-30","source":"xxxxxxxxxxxxxxxxxxxxx","value":1},{"date":"2012-06-01","source":"xxxxxxxxxxxxxxxxxxxx","value":1},{"date":"2012-06-08","source":"xxxxxxxxxxxxxxxxxxxxxx","value":1}]'
    

答案 1 :(得分:0)

虽然如果转换为字符串(如其他问题和评论中所述),您可以获得所需的格式,您可能不需要。查看 to_json() parameter date_format 。我相信你想要.to_json(..., date_format='iso')

根据date_format参数的文档:

  

对于orient ='table',默认为'iso'。对于所有其他方位,默认为'epoch'。