无法使用 to_json 将 Pandas DataFrame 转换为 json
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26823278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to convert pandas DataFrame to json using to_json
提问by CM-Dev
I am aware that there are several other posts on Stack Overflow regarding this same issue, however, not a single solution found on those posts, or any other post I've found online for that matter, has worked. I have followed numerous tutorials, videos, books, and Stack Overflow posts on pandas and all mentioned solutions have failed.
我知道 Stack Overflow 上还有其他几篇关于同一问题的帖子,但是,在这些帖子或我在网上找到的任何其他帖子都没有找到任何解决方案。我已经阅读了大量关于 Pandas 的教程、视频、书籍和 Stack Overflow 帖子,但所有提到的解决方案都失败了。
The frustrating thing is that all the solutions I have found are correct, or at least they should be; I am fairly new to pandas so my only conclusion is that I am probably doing something wrong.
令人沮丧的是,我找到的所有解决方案都是正确的,或者至少应该是正确的;我对大Pandas相当陌生,所以我唯一的结论是我可能做错了什么。
Here is the pandas documentation that I started with: Pandas to_json Doc. I can't seem to get pandas to_json to convert a pandas DataFrame to a json object or json string.
这是我开始使用的 pandas 文档:Pandas to_json Doc。我似乎无法让pandas to_json 将pandas DataFrame 转换为json 对象或json 字符串。
Basically, I want to convert a csv string into a DataFrame, then convert that DataFrame into a json object or json string (I don't care which one). Then, once I have my json data structure, I'm going to bind it to a D3.js bar chart
基本上,我想将 csv 字符串转换为 DataFrame,然后将该 DataFrame 转换为 json 对象或 json 字符串(我不在乎是哪一个)。然后,一旦我有了我的 json 数据结构,我将把它绑定到一个 D3.js 条形图
Here is an example of what I am trying to do:
这是我正在尝试做的一个例子:
# Declare my csv string (Works):
csvStr = '"pid","dos","facility","a1c_val"\n"123456","2013-01-01 13:37:00","UOFU",5.4\n"65432","2014-01-01 14:32:00","UOFU",5.8\n"65432","2013-01-01 13:01:00","UOFU",6.4'
print (csvStr) # Just checking the variables contents
# Read csv and convert to DataFrame (Works):
csvDf = pandas.read_csv(StringIO.StringIO(csvStr))
print (csvDf) # Just checking the variables contents
# Convert DataFrame to json (Three of the ways I tried - None of them work):
myJSON = csvDf.to_json(path_or_buf = None, orient = 'record', date_format = 'epoch', double_precision = 10, force_ascii = True, date_unit = 'ms', default_handler = None) # Attempt 1
print (myJSON) # Just checking the variables contents
myJSON = csvDf.to_json() # Attempt 2
print (myJSON) # Just checking the variables contents
myJSON = pandas.io.json.to_json(csvDf)
print (myJSON) # Just checking the variables contents
The error that I am getting is:
我得到的错误是:
argument 1 must be string or read-only character buffer, not DataFrame
参数 1 必须是字符串或只读字符缓冲区,而不是 DataFrame
Which is misleading because the documentation says "A Series or DataFrame can be converted to a valid JSON string."
这是误导性的,因为文档说“A Series 或 DataFrame 可以转换为有效的 JSON 字符串”。
Regardless, I tried giving it a string anyway, and it resulted in the exact same error.
无论如何,我还是尝试给它一个字符串,但结果却是完全相同的错误。
I have tried creating a test scenario, following the exact steps from books and other tutorials and/or posts and it just results in the same error. At this point, I need a simple solution asap. I am open to suggestions, but I must emphasize that I do not have time waste on learning a completely new library.
我尝试按照书籍和其他教程和/或帖子中的确切步骤创建一个测试场景,但它只会导致相同的错误。在这一点上,我需要尽快找到一个简单的解决方案。我愿意接受建议,但我必须强调,我没有时间浪费在学习一个全新的图书馆上。
回答by Dair
For you first attempt, the correct string is 'records'not 'record'This worked for me:
对于您第一次尝试,正确的字符串'records'不是'record'这对我有用:
myJSON = csvDf.to_json(path_or_buf = None, orient = 'records', date_format = 'epoch', double_precision = 10, force_ascii = True, date_unit = 'ms', default_handler = None) # Attempt 1
Printing gives:
印刷给出:
[{"pid":123456,"dos":"2013-01-01 13:37:00","facility":"UOFU","a1c_val":5.4},
{"pid":65432,"dos":"2014-01-01 14:32:00","facility":"UOFU","a1c_val":5.8},
{"pid":65432,"dos":"2013-01-01 13:01:00","facility":"UOFU","a1c_val":6.4}]
回答by CM-Dev
It turns out that the problem was becuase of my own stupid mistake. While testing my use of to_json, I copy and pasted an example into my code and went from there. Thinking I had commented out that code, I proceeded to try using to_json with my test data. Turns out the error I was receiving was being thrown from the example code that I had copy and pasted. Once I deleted everything and re-wrote it using my test data it worked.
事实证明,问题是因为我自己的愚蠢错误。在测试我对 to_json 的使用时,我将一个示例复制并粘贴到我的代码中,然后从那里开始。以为我已经注释掉了该代码,我继续尝试将 to_json 与我的测试数据一起使用。原来我收到的错误是从我复制和粘贴的示例代码中抛出的。一旦我删除了所有内容并使用我的测试数据重新编写它,它就起作用了。
However, as user667648 (Bair) pointed out, there was another mistake in my code. The orientparam was suppose to be orient = 'records'and NOT orient = 'record'.
但是,正如 user667648 (Bair) 指出的那样,我的代码中还有另一个错误。该orientPARAM被假设是orient = 'records'与不是orient = 'record'。

