我在python中使用pandas获取csv文件,对其进行一些小的转换,然后将这两列作为json文件输出。我想要两个值Utility.SqlNumericMax(19, 6)
和timestamp
。我只想要两个新列并删除文件的其余部分,使其看起来像:
value
但是现在我的代码我仍然得到所有旧的csv数据,我想要的部分被附加(并且格式):
{"timestamp[0]":value[0],"timestamp[1]":value[1],"timestamp[2]":value[2],..}
这是我目前正在使用的代码:
{stuff I don't want, "timestamp":"timestamp[0]", "value":value[0]},{...}{...}
任何帮助将不胜感激
因此,基于Turn的帮助,我根据他的建议更改了代码。现在我得到输出:
import csv
import pandas as pd
import delorean as dl
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)
f["timestamp"] = hmCols["timestamp"] #old code
f["value"] = hmCols["value"] #old code
f.to_json(outfile, orient="records") #old code
pd.DataFrame(hmCols).to_json(outfile, orient="records") #From user Turn
doThings("test.csv", "heatmapData.json")
现在我需要将其转换为:
[{"timestamp":1417982808063,"value":1},{"timestamp":1417982808063,"value":1},{"timestamp":1417982808753,"value":1},{"timestamp":1417982811944,"value":1} ...
答案 0 :(得分:1)
如果你改变这个怎么办:
f["timestamp"] = hmCols["timestamp"]
f["value"] = hmCols["value"]
f.to_json(outfile, orient="records")
为:
pd.DataFrame(hmCols).to_json(outfile, orient="records")
编辑以添加:
我误解了你要找的输出。如果您将整个循环更改为此内容(顶部添加了import json
)该怎么办:
def doThings(infile, outfile):
f = pd.read_csv(infile)
result = []
for i, row in f.iterrows():
total = row["Playspace_1"] + row["Playspace_2"] + row["Playspace_3"] + row["Playspace_4"]
result.append({row["Timestamp"]: total})
with open(outfile, 'w') as fp:
json.dump(result, fp)