pandas 数据框作为嵌入文档(目录列表)插入到 MongoDB
pandas dataframe insert to MongoDB as embedded document (list of Directories)
我在插入 MongoDB 之前的数据框如下所示,
code trade_date open high low close pre_close change pct_chg vol
0 600111 20210308 58.99 59.16 56.58 56.66 58.52 -1.86 -3.1784 299902.37
1 600111 20210305 58.00 58.91 57.71 58.52 59.31 -0.79 -1.3320 281584.57
2 600111 20210304 60.31 61.60 58.96 59.31 58.67 0.64 1.0908 621415.96
3 600111 20210303 58.21 58.80 57.80 58.67 58.49 0.18 0.3077 235677.52
我从 trade_date 中提取了年份和月份,并添加了一个新列“trade_month”,如下所示,
code trade_date open high low close pre_close change pct_chg vol trade_month
0 600111 20210308 58.99 59.16 56.58 56.66 58.52 -1.86 -3.1784 299902.37 202103
1 600111 20210305 58.00 58.91 57.71 58.52 59.31 -0.79 -1.3320 281584.57 202103
如何通过 Pymongo 将此 table 插入到 Mongodb 中(嵌入 mongodb 文档,包括整个月的交易数据作为字典列表)?
{
'code':'600111',
'trade_month':'202103',
{'month_records':
[
{'trade_date':'20210301','open':'123', 'high':'123',...},
{'trade_date':'20210302','open':'123', 'high':'123',...},
{'trade_date':'20210303','open':'123', 'high':'123',...},
...
]
}
}
我想要一个月的记录,因为单个 MongoDB 文档与我的 MongoDB 模式设计保持一致,因为我通常通过代码和 trade_date 检索 1 个月的数据在 MongoDB.
中编入索引
我自己研究了将近2天,没有找到任何关于如何实现的线索。如有帮助,不胜感激。
在 code
和 month
级别使用 groupby 并聚合到 dict。
df.groupby(['code', 'trade_month']).apply(
lambda x: x.to_dict(orient='records')
).rename('month_records').reset_index().to_dict('records')
输出:
[
{
"code":600111,
"trade_month":202103,
"month_records":[
{
"code":600111,
"trade_date":20210308,
"open":58.99,
"high":59.16,
"low":56.58,
"close":56.66,
"pre_close":58.52,
"change":-1.86,
"pct_chg":-3.1784,
"vol":299902.37,
"trade_month":202103
},
{
"code":600111,
"trade_date":20210305,
"open":58.0,
"high":58.91,
"low":57.71,
"close":58.52,
"pre_close":59.31,
"change":-0.79,
"pct_chg":-1.332,
"vol":281584.57,
"trade_month":202103
}
]
}
]
我在插入 MongoDB 之前的数据框如下所示,
code trade_date open high low close pre_close change pct_chg vol
0 600111 20210308 58.99 59.16 56.58 56.66 58.52 -1.86 -3.1784 299902.37
1 600111 20210305 58.00 58.91 57.71 58.52 59.31 -0.79 -1.3320 281584.57
2 600111 20210304 60.31 61.60 58.96 59.31 58.67 0.64 1.0908 621415.96
3 600111 20210303 58.21 58.80 57.80 58.67 58.49 0.18 0.3077 235677.52
我从 trade_date 中提取了年份和月份,并添加了一个新列“trade_month”,如下所示,
code trade_date open high low close pre_close change pct_chg vol trade_month
0 600111 20210308 58.99 59.16 56.58 56.66 58.52 -1.86 -3.1784 299902.37 202103
1 600111 20210305 58.00 58.91 57.71 58.52 59.31 -0.79 -1.3320 281584.57 202103
如何通过 Pymongo 将此 table 插入到 Mongodb 中(嵌入 mongodb 文档,包括整个月的交易数据作为字典列表)?
{
'code':'600111',
'trade_month':'202103',
{'month_records':
[
{'trade_date':'20210301','open':'123', 'high':'123',...},
{'trade_date':'20210302','open':'123', 'high':'123',...},
{'trade_date':'20210303','open':'123', 'high':'123',...},
...
]
}
}
我想要一个月的记录,因为单个 MongoDB 文档与我的 MongoDB 模式设计保持一致,因为我通常通过代码和 trade_date 检索 1 个月的数据在 MongoDB.
中编入索引我自己研究了将近2天,没有找到任何关于如何实现的线索。如有帮助,不胜感激。
在 code
和 month
级别使用 groupby 并聚合到 dict。
df.groupby(['code', 'trade_month']).apply(
lambda x: x.to_dict(orient='records')
).rename('month_records').reset_index().to_dict('records')
输出:
[
{
"code":600111,
"trade_month":202103,
"month_records":[
{
"code":600111,
"trade_date":20210308,
"open":58.99,
"high":59.16,
"low":56.58,
"close":56.66,
"pre_close":58.52,
"change":-1.86,
"pct_chg":-3.1784,
"vol":299902.37,
"trade_month":202103
},
{
"code":600111,
"trade_date":20210305,
"open":58.0,
"high":58.91,
"low":57.71,
"close":58.52,
"pre_close":59.31,
"change":-0.79,
"pct_chg":-1.332,
"vol":281584.57,
"trade_month":202103
}
]
}
]