将数据框转换为JSON,但将列作为数组

时间:2019-02-21 11:32:21

标签: python json pandas dataframe

以下代码:

import pandas as pd

d = {
    'col1': [1, 2, 3],
    'col2': [4, 5, 6],
}

df = pd.DataFrame(d).to_json()

print(df)

给予:

{"col1":{"0":1,"1":2,"2":3},"col2":{"0":4,"1":5,"2":6}}

如何禁用枚举并将值转换为数组,所以输出是这样的?:

{"col1": [1, 2, 3], "col2": [4, 5, 6]}

0 个答案:

没有答案