将列值用作大熊猫JSON中的键
我有下面的熊猫数据框架。我只是想知道是否有任何方法可以将我的列值作为JSON的钥匙。
DF: |符号|价格| |:------- | ------- | | a。 | 120 | | b。 | 100 | | C | 200 |
我希望json看起来像{'a':120,'b':100,'c':200}
我尝试了下面的结果,并将结果作为{符号:'a',价格:120} { :'b',价格:100} {符号:'c',价格:200}
df.to_json('price.json', orient='records', lines=True)
I have a pandas dataframe as below. I'm just wondering if there's any way to have my column values as my key to the json.
df:
|symbol | price|
|:------|------|
|a. |120|
|b. |100|
|c |200|
I expect the json to look like {'a': 120, 'b': 100, 'c': 200}
I've tried the below and got the result as {symbol: 'a', price: 120}{symbol: 'b', price: 100}{symbol: 'c', price: 200}
df.to_json('price.json', orient='records', lines=True)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,让我们创建OP提及的数据
框 以下工作将完成工作,以下工作将完成工作
pandas-json/72259719#comment127663214_72259545“>在此处注释),如果一个人想要json值作为列表,
Let's start by creating the dataframe that OP mentions
Considering that OP doesn't want the JSON values as a list (as OP commented here), the following will do the work
If one wants the JSON values as a list, the following will do the work
尝试这样:
输出:
Try like this :
Output :