pandas 熊猫行到 json
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36051134/
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
Pandas row to json
提问by Roger Josh
I have a dataframe in pandas and my goal is to write each row of the dataframe as a new json file.
我在 Pandas 中有一个数据框,我的目标是将数据框的每一行都写成一个新的 json 文件。
I'm a bit stuck right now. My intuition was to iterate over the rows of the dataframe (using df.iterrows) and use json.dumps to dump the file but to no avail.
我现在有点卡住了。我的直觉是遍历数据帧的行(使用 df.iterrows)并使用 json.dumps 转储文件但无济于事。
Any thoughts?
有什么想法吗?
采纳答案by tvashtar
Pandas DataFrames have a to_json method that will do it for you: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_json.html
Pandas DataFrames 有一个 to_json 方法可以为您完成:http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_json.html
If you want each row in its own file you can iterate over the index (and use the index to help name them):
如果您希望每一行都在自己的文件中,您可以遍历索引(并使用索引来帮助命名它们):
for i in df.index:
df.loc[i].to_json("row{}.json".format(i))
回答by MrE
Looping over indices is very inefficient.
循环索引非常低效。
A faster technique:
更快的技术:
df['json'] = df.apply(lambda x: x.to_json(), axis=1)
df['json'] = df.apply(lambda x: x.to_json(), axis=1)
回答by Steni Thomas
Using apply, this can be done as
使用apply,这可以做为
def writejson(row):
with open(row["filename"]+'.json', "w") as outfile:
json.dump(row["json"], outfile, indent=2)
in_df.apply(writejson, axis=1)
Assuming the dataframe has a column named "filename" with filename for each json row.
假设数据框有一个名为“filename”的列,每个 json 行都有文件名。