作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试生成一个 JSON blob,其中包含从我的数据框中的一系列数据生成的列表。当我运行 to_json(orient='values')
时,它适本地将系列转换为仅包含系列值的列表,但随后将列表用单引号引起来,如果有人处理,则很难解析此数据它。有没有一种好方法可以将系列转换为 JSON 对象,但不将列表用单引号引起来?
代码:
# Create empty JSON blob
output_json = {}
# Add to JSON output
output_json['count_time_fitness_timeseries'] = {
'x': df.fitness_date.to_json(orient='values'),
'y': df.minutes_fitness.to_json(orient='values'),
'labels': df.minutes_fitness_hhmm.to_json(orient='values')
}
print(output_json)
例子:
{
'count_time_fitness_timeseries': {
'x': '["2020-04-01","2020-04-02","2020-04-03","2020-04-04","2020-04-05","2020-04-06","2020-04-07","2020-04-08","2020-04-09","2020-04-10","2020-04-11","2020-04-12","2020-04-13","2020-04-14","2020-04-15","2020-04-16","2020-04-17","2020-04-18","2020-04-19","2020-04-20","2020-04-21","2020-04-22","2020-04-23","2020-04-24","2020-04-25","2020-04-26","2020-04-27","2020-04-28","2020-04-29","2020-04-30","2020-05-01","2020-05-02","2020-05-03","2020-05-04","2020-05-05","2020-05-06","2020-05-07","2020-05-08","2020-05-09","2020-05-10","2020-05-11","2020-05-12","2020-05-13","2020-05-14","2020-05-15","2020-05-16","2020-05-17","2020-05-18","2020-05-19","2020-05-20","2020-05-21","2020-05-22","2020-05-23","2020-05-24","2020-05-25","2020-05-26","2020-05-27","2020-05-28","2020-05-29","2020-05-30","2020-05-31","2020-06-01","2020-06-02","2020-06-03","2020-06-04","2020-06-05","2020-06-06","2020-06-07","2020-06-08","2020-06-09","2020-06-10","2020-06-11","2020-06-12","2020-06-13","2020-06-14","2020-06-15","2020-06-16","2020-06-17","2020-06-18","2020-06-19","2020-06-20","2020-06-21","2020-06-22","2020-06-23","2020-06-24","2020-06-25","2020-06-26","2020-06-27","2020-06-28","2020-06-29","2020-06-30","2020-07-01"]',
'y': '[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,461.0,421.0,519.0,502.0,511.0,513.0,496.0,480.0,364.0,498.0,467.0,477.0,431.0,419.0,471.0,391.0,481.0,494.0,506.0,464.0,474.0,383.0,385.0,470.0,465.0,574.0,473.0,431.0,497.0,null,482.0,492.0,494.0,469.0,395.0,427.0,346.0,416.0,461.0,486.0,451.0,533.0,null,462.0,461.0,477.0,458.0,484.0,389.0,null,472.0,462.0,486.0,489.0,483.0,426.0,453.0,489.0,467.0,474.0,451.0,450.0,470.0,null,247.0,502.0,464.0]',
'labels': '[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,"7:41","7:01","8:39","8:22","8:31","8:33","8:16","8:00","6:04","8:18","7:47","7:57","7:11","6:59","7:51","6:31","8:01","8:14","8:26","7:44","7:54","6:23","6:25","7:50","7:45","9:34","7:53","7:11","8:17",null,"8:02","8:12","8:14","7:49","6:35","7:07","5:46","6:56","7:41","8:06","7:31","8:53",null,"7:42","7:41","7:57","7:38","8:04","6:29",null,"7:52","7:42","8:06","8:09","8:03","7:06","7:33","8:09","7:47","7:54","7:31","7:30","7:50",null,"4:07","8:22","7:44"]'
}
最佳答案
试试这个:
import json
#.... your code
print(json.dumps(output_json))
关于python - 防止 Pandas to_json() 将列表用单引号引起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62980921/
我是一名优秀的程序员,十分优秀!