【问题标题】:Pandas Series to json and back熊猫系列到 json 和返回
【发布时间】:2014-05-22 20:35:31
【问题描述】:

我在将简单的 Pandas 系列转换为 json 字符串并返回时遇到了一些问题。这是我的尝试

import pandas as pd
f = pd.Series(data=[1.0,2.0,3.0],index=[10,20,30])
x = f.to_json()
a = pd.read_json(x)

这会导致 ValueError: If using all scalar values, you must pass an Index.

json 字符串 x 看起来像 {"10":1.0,"20":2.0,"30":3.0}

这里缺少什么。请帮忙

【问题讨论】:

    标签: python json pandas


    【解决方案1】:

    您需要指定对象的类型(默认为DataFrame)和JSON字符串的格式。更多信息here

    这应该可行:

    a = pd.read_json(x, typ='series', orient='records')
    

    【讨论】:

    • 啊,我太笨了。我监督了 typ 论点......现在很容易。作品。非常感谢
    猜你喜欢
    • 2020-06-03
    • 1970-01-01
    • 2014-06-15
    • 1970-01-01
    • 2020-03-30
    • 1970-01-01
    • 1970-01-01
    • 2016-09-23
    • 2018-02-09
    相关资源
    最近更新 更多