python-3.x - 如何根据 ID 合并两个大型 Pandas 数据帧 - 通过块读取最大的文件?
问题描述
我有两个 CSV 文件,df1_50GB.csv 和 df2_1GB.csv。我想根据匹配的 ID 值合并数据框。这是我到目前为止所做的
f50GB = "df1_50GB.csv"
f1GGB = "df1_1GGB.csv"
result_file = "output.csv"
tmp50GB = pd.read_csv(f50GB, nrows=3)
tmp1GB = pd.read_csv(f1GGB, nrows=3)
# creating an empty bucket to save result
df_result = pd.DataFrame(columns=(tmp50GB.columns.append(tmp1GB.columns)).unique())
df_result.to_csv(result_file, index_label=False)
tmp1GB = pd.read_csv(f1GGB)
# Save (append) data which the ID of f1GB mached with f50GB i.e (x) #
def save_merged_csv(x):
df = pd.merge(x, tmp1GB, on='ID', how='left')
df.to_csv(result_file, mode="a", header=False, index=False)
# create the iterator
csv_reader = pd.read_csv(
f50GB,
iterator=True,
chunksize=100000)
[save_merged_csv(r) for r in csv_reader]
但我不断收到错误“TypeError:只能合并 Series 或 DataFrame 对象,a 已通过”[更新]该错误现已修复。感谢@Michael Gardner。但我不确定逻辑是否正确。结果的文件大小太大。它也很慢。所以,如果有改进的余地,我会把它留在这里。
解决方案
推荐阅读
- python - 带有 if/else 和 append 函数的 Python for 循环
- sql-server-2008 - SQL Server:检查两列之间范围内的值
- marklogic - MarkLogic - 使用搜索 API 查询多个输入值
- kotlin - Kotlin 反射:如何知道 Kotlin 类是否标有“内部”可见性修饰符
- android - ImageView 未在 ItemList 中更新
- python - Flask - 类型错误:在 Flask-SQLAlchemy 中的“BaseQuery”和“int”实例之间不支持“>”
- r - 在 R 中生成 3.000.000 个长度为 11 的字符串
- c - 从管道读取的子进程失败并且似乎出现故障
- matlab - 如何在曲线拟合工具箱(Matlab)中添加自由参数?
- qt - 如何在 Stackview QML 中收听来自 initalItem 的信号