簡體   English   中英

每次合並具有不同列名的 pandas 數據幀列表

[英]Merge a list of pandas dataframes WITH different column names each time

我有一個數據框列表,其中包含要加入的相應列名,例如:

dfs = [df1, df2, df3, df4]

col_join = ["col1", "col2", "col3"]

我已經看到在 Python 中使用減少 function 的答案。

import pandas as pd
from functools import reduce
reduce(lambda x, y: pd.merge(x, y, on=["Col1"], how="outer"), dfs)

如何合並合並列在reduce function 內的每個連接處發生變化的事實?

先感謝您。

這可能有效(未經測試):

result = df1
for df, col in zip(dfs[1:], col_join):
    result = pd.merge(result, df, on=[col], how='outer')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM