问题描述
鉴于下面的示例 CSV 数据,在 Pandas DataFrame 中,我如何输出 to_json 如下
as_of
category
type
subtype
log: [
#sorted by timestamp
{timestamp: 1618879229,action: add,stale_timestamp: true},{timestamp: 1608879229,action: remove,stale_timestamp: None},]
20210415
apples
A
big
log: [
{timestamp: 1618879229,]
small
log: [
{timestamp: 1618879229,{timestamp: 1518879229,]
B
big
log: [
{timestamp: 1618879229,]
如果您还可以帮助我从嵌套的 json 中返回到 DataFrame,那就太棒了!
as_of | 类别 | 类型 | 子类型 | 动作 | 时间戳 | stale_timestamp |
---|---|---|---|---|---|---|
20210415 | 苹果 | A | 大 | 添加 | 1618879229.6703315 | |
20210415 | 苹果 | A | 小 | 添加 | 1618879229.6703315 | |
20210415 | 苹果 | B | 大 | 添加 | 1618879229.6703315 | |
20210415 | 苹果 | B | 小 | 添加 | 1618879229.6703315 | |
20210415 | 苹果 | C | 大 | 添加 | 1618879229.6703315 | |
20210415 | 苹果 | C | 小 | 添加 | 1618879229.6703315 | |
202103 | 橙子 | 甜的 | 添加 | 1616892142.6703315 | ||
202103 | 橙子 | 甜的 | 删除 | 1616632942.6703315 | ||
202103 | 橙子 | 甜的 | 添加 | 1616200942.6703315 | ||
202103 | 葡萄 | 甜的 | 添加 | 1616200942.6703315 | ||
202102 | 橙子 | 甜的 | 添加 | 1616200942.6703315 | ||
202102 | 葡萄 | 甜的 | 添加 | 1616200942.6703315 | ||
20210115 | 苹果 | A | 大 | 添加 | 1611103342.6703315 | |
20210115 | 苹果 | A | 小 | 添加 | 1611103342.6703315 | |
20210115 | 苹果 | B | 大 | 添加 | 1611103342.6703315 | |
20210115 | 苹果 | B | 小 | 添加 | 1611103342.6703315 | |
20210115 | 苹果 | C | 大 | 添加 | 1611103342.6703315 | |
20210115 | 苹果 | C | 小 | 添加 | 1611103342.6703315 | |
202101 | 橙子 | 甜的 | 添加 | 1608424942.6703315 | ||
202101 | 葡萄 | 甜的 | 添加 | 1608424942.6703315 | ||
202012 | 橙子 | 甜的 | 添加 | 1608424942.6703315 | ||
202012 | 葡萄 | 甜的 | 添加 | 1608424942.6703315 | ||
202011 | 橙子 | 甜的 | 添加 | 1608424942.6703315 | ||
202011 | 葡萄 | 甜的 | 添加 | 1608424942.6703315 | ||
20201015 | 苹果 | A | 大 | 添加 | 1608424942.6703315 | 真的 |
20201015 | 苹果 | A | 小 | 添加 | 1608424942.6703315 | 真的 |
20201015 | 苹果 | B | 大 | 添加 | 1608424942.6703315 | 真的 |
20201015 | 苹果 | B | 小 | 添加 | 1608424942.6703315 | 真的 |
20201015 | 苹果 | C | 大 | 添加 | 1608424942.6703315 | 真的 |
20201015 | 苹果 | C | 小 | 添加 | 1608424942.6703315 | 真的 |
202010 | 橙子 | 甜的 | 添加 | 1608424942.6703315 | 真的 | |
202010 | 葡萄 | 甜的 | 添加 | 1608424942.6703315 | 真的 |
解决方法
首先我将表格转换为 CSV:
as_of,category,type,sub_type,action,timestamp,stale_timestamp
20210415,apples,A,big,add,1618879230,20210415,small,B,C,202103,oranges,sweet,1616892143,remove,1616632943,1616200943,grapes,202102,20210115,1611103343,202101,1608424943,202012,202011,20201015,TRUE
20201015,TRUE
202010,TRUE
缺失的条目会导致稍后在 JSON 中出现问题。这需要在输入文件或 Python 转换器中修复。此外,某些日期似乎缺少字符。
因为 Pandas 中没有预定义的 orient
选项适合这个要求,所以我编写了一个自定义字典,然后将字典转换为 JSON。
import pandas
import json
df = pandas.read_csv('sheet1.csv',header=None,dtype=str)
mydic = {}
for unique_col0 in df[0].unique():
mydic[unique_col0] = {}
sub_df = df[df[0]==unique_col0]
for unique_col1 in sub_df[1].unique():
mydic[unique_col0][unique_col1] = {}
sub_sub_df = sub_df[sub_df[1]==unique_col1]
for unique_col2 in sub_sub_df[2].unique():
mydic[unique_col0][unique_col1][unique_col2] = {}
sub_sub_sub_df = sub_sub_df[sub_sub_df[2]==unique_col2]
for unique_col3 in sub_sub_sub_df[3].unique():
mydic[unique_col0][unique_col1][unique_col2][unique_col3] = {'log':[]}
for index in range(sub_sub_sub_df.shape[0]):
this_dict = {'timestamp': list(sub_sub_sub_df[5])[index],'action': list(sub_sub_sub_df[4])[index],'stale_timestamps': list(sub_sub_sub_df[6])[index]}
mydic[unique_col0][unique_col1][unique_col2][unique_col3]['log'].append(this_dict)
with open('output.json','w') as file_handle:
json.dump(mydic,file_handle,indent=2)
提问者提供的示例输出与 Python 实现实际生成的结果不一致。