【发布时间】:2020-03-22 09:05:36
【问题描述】:
我正在寻找涉及 venmo 数据集的项目。我能够下载 bson 文件,它位于我的桌面上,但我不知道如何处理它。我对 MongoDB 不太熟悉,我希望将其转换为 pandas 数据框进行分析。有人知道这样做的任何提示吗?
【问题讨论】:
标签: bson
我正在寻找涉及 venmo 数据集的项目。我能够下载 bson 文件,它位于我的桌面上,但我不知道如何处理它。我对 MongoDB 不太熟悉,我希望将其转换为 pandas 数据框进行分析。有人知道这样做的任何提示吗?
【问题讨论】:
标签: bson
在下面找到如何读取 bson 文件的 Python 示例:
import pandas as pd
import bson
FILE="/folder/file.bson"
with open(FILE,'rb') as f:
data = bson.decode_all(f.read())
main_df=pd.DataFrame(data)
main_df.describe()
【讨论】:
pip install pymongo 的 import bson 时有效,请注意它不适用于 pip install bson 的 import bson。如果你碰巧同时安装了这两个,pip install pymongo 的import bson 会比pip install bson 占优势,但是你也可以使用pip uninstall bson。如果您需要这两个包,请使用pip install pybson,然后使用from pybson import bson as ...,替代名称根据github.com/py-bson/bson/issues/70
b = open(mongodbbsonfilename, 'rb').read()bs = bson.loads(b)data = bson.decode_binary_subtype( bs, 2 )df = pd.DataFrame.from_dict(pd.json_normalize(data), orient='columns') 当我将 read() 更改为 readfiles() 时,它不再是 BSON String,但是一个列表。
TypeError: a bytes-like object is required, not 'list'。我尝试使用与此 JSON 问题类似的方法将该列表(先前的评论)转换为 BSON 字符串:然后我尝试了类似的 JSON 方法,但没有成功:pythonpedia.com/en/knowledge-base/48614158/…