【发布时间】:2021-04-20 02:08:14
【问题描述】:
鉴于下面的示例 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, action: add, stale_timestamp: None},
]
small
log: [
{timestamp: 1618879229, action: add, stale_timestamp: None},
{timestamp: 1608879229, action: remove, stale_timestamp: None},
{timestamp: 1518879229, action: add, stale_timestamp: None},
]
B
big
log: [
{timestamp: 1618879229, action: add, stale_timestamp: None},
]
如果您还可以帮助我从嵌套的 json 中返回 DataFrame,那么奖励!
| as_of | category | type | sub_type | action | timestamp | stale_timestamp |
|---|---|---|---|---|---|---|
| 20210415 | apples | A | big | add | 1618879229.6703315 | |
| 20210415 | apples | A | small | add | 1618879229.6703315 | |
| 20210415 | apples | B | big | add | 1618879229.6703315 | |
| 20210415 | apples | B | small | add | 1618879229.6703315 | |
| 20210415 | apples | C | big | add | 1618879229.6703315 | |
| 20210415 | apples | C | small | add | 1618879229.6703315 | |
| 202103 | oranges | sweet | add | 1616892142.6703315 | ||
| 202103 | oranges | sweet | remove | 1616632942.6703315 | ||
| 202103 | oranges | sweet | add | 1616200942.6703315 | ||
| 202103 | grapes | sweet | add | 1616200942.6703315 | ||
| 202102 | oranges | sweet | add | 1616200942.6703315 | ||
| 202102 | grapes | sweet | add | 1616200942.6703315 | ||
| 20210115 | apples | A | big | add | 1611103342.6703315 | |
| 20210115 | apples | A | small | add | 1611103342.6703315 | |
| 20210115 | apples | B | big | add | 1611103342.6703315 | |
| 20210115 | apples | B | small | add | 1611103342.6703315 | |
| 20210115 | apples | C | big | add | 1611103342.6703315 | |
| 20210115 | apples | C | small | add | 1611103342.6703315 | |
| 202101 | oranges | sweet | add | 1608424942.6703315 | ||
| 202101 | grapes | sweet | add | 1608424942.6703315 | ||
| 202012 | oranges | sweet | add | 1608424942.6703315 | ||
| 202012 | grapes | sweet | add | 1608424942.6703315 | ||
| 202011 | oranges | sweet | add | 1608424942.6703315 | ||
| 202011 | grapes | sweet | add | 1608424942.6703315 | ||
| 20201015 | apples | A | big | add | 1608424942.6703315 | True |
| 20201015 | apples | A | small | add | 1608424942.6703315 | True |
| 20201015 | apples | B | big | add | 1608424942.6703315 | True |
| 20201015 | apples | B | small | add | 1608424942.6703315 | True |
| 20201015 | apples | C | big | add | 1608424942.6703315 | True |
| 20201015 | apples | C | small | add | 1608424942.6703315 | True |
| 202010 | oranges | sweet | add | 1608424942.6703315 | True | |
| 202010 | grapes | sweet | add | 1608424942.6703315 | True |
【问题讨论】:
-
这实际上是 Pandas 的
.to_json函数中的自定义orient。提供的表格作为 CSV 会更有用。