【问题标题】:Merging two json key value paris into a new key value pair in Python在Python中将两个json键值paris合并成一个新的键值对
【发布时间】:2015-12-10 03:49:48
【问题描述】:

因此,根据一些反馈,我稍微扩展了代码,因为我实际上是在使用更大的 csv 并首先对其进行转换。我试图输出的实际上是转换 - 相关代码:

def doThings(infile, outfile):
    f = pd.read_csv(infile)
    hmCols = {"timestamp": [], "value": []}    
for i, row in f.iterrows():
    total = row["Playspace_1"] + row["Playspace_2"] + row["Playspace_3"] + row["Playspace_4"]
    hmCols["timestamp"].append(row["Timestamp"])
    hmCols["value"].append(total)


pd.DataFrame(hmCols).to_json(outfile, orient="records")
doThings("test.csv", "heatmapData.json")

现在它输出为:

[{"timestamp":1417982808063,"value":1},{"timestamp":1417982808063,"value":1},{"timestamp":1417982808753,"value":1},{"timestamp":1417982811944,"value":1}...]

我需要的是它是这样的:

[{"1417982808063":1},{"1417982808063":1},{"1417982808753":1},{"1417982811944":1}...]

任何有关如何进行此转换的帮助将不胜感激

【问题讨论】:

  • 抱歉,这只是一个愚蠢的错字 - 已修复
  • 你在这里使用熊猫有什么特殊原因吗?
  • 老实说——我们最初打算用它来做可视化(热图)——但出于很多原因,我们现在要使用 D3 ......我现在不一定喜欢
  • 这是定期生成的文件吗?还是每次向 URL 端点提交请求时生成?
  • 这是旧数据,我正在从日志文件中提取它。所以每次我提交请求时都会生成它 - 这不是实时的

标签: python json pandas


【解决方案1】:
dic_in = [
    {"timestamp":1417982808063,"value":1},
    {"timestamp":1417982808063,"value":1},
    {"timestamp":1417982808753,"value":1},
    {"timestamp":1417982811944,"value":1}
]

dic_out = [{i['timestamp']: i['value']} for i in dic_in]

【讨论】:

  • 当我尝试这个时,我得到了错误:TypeError: string indices must be integers, not str
  • @Mike 你的 Python 版本是什么?
  • 我试过这样 f = pd.DataFrame(hmCols).to_json(orient = "records") dic_out = [{i['timestamp']: i['value']} for i in f] print dic_out 和:dic_out = [{i['timestamp']: i['value']} for i in hmCols] print dic_out 但在这两种情况下都得到了 TypeError
【解决方案2】:

...只需使用csv.reader

  f = csv.reader(some_file_handle)
  next(f) #skip header
  json.dump(dict((row[0],sum(map(int,row[1:]))) for row in f),open("output.json","wb"))  

我认为至少......没有测试

这假设 in_file.csv 看起来像

timestamp,Playspace_1,Playspace_2,Playspace_3,Playspace_4
123      ,0          ,1          ,0          ,0
....

(根据您对原始问题的评论,我取消了此答案)

如果你真的嫁给了熊猫,我猜你可以做类似的事情

totals = f["Playspace_1"] + f["Playspace_2"] + f["Playspace_3"] + f["Playspace_4"]
json.dump(dict(zip(f["timestamp"],totals),open("out.json","wb")))

【讨论】:

  • 哎呀有一个错字缺少括号
  • 我还建议使用像 memcache 这样的缓存机制来存储您的结果,这样它只会在输入文件发生更改时重新计算...
猜你喜欢
  • 1970-01-01
  • 2021-04-01
  • 2010-09-14
  • 1970-01-01
  • 2015-06-12
  • 2011-03-25
  • 2011-10-18
相关资源
最近更新 更多