dear sir
I am attempting to perform an inner join on multiple dataframes with differing columns using the ‘from functools import reduce’ library. However, I am encountering errors with my code. I have tried two methods, but both resulted in errors. Please review my code and the associated error messages, and provide the correct solution and code for performing an inner join on multiple dataframes with different columns, similar to an Oracle inner join.
import streamlit as st
import pandas as pd
from functools import reduce
# Sample DataFrames
df1 = pd.DataFrame({‘id_1’: [1, 2], ‘val1’: [‘A’, ‘B’]})
df2 = pd.DataFrame({‘id_2’: [1, 2], ‘val2’: [‘X’, ‘Y’]})
df3 = pd.DataFrame({‘id_3’: [1, 2], ‘val3’: [‘M’, ‘N’]})
# List your DataFrames
dfs = [df1, df2, df3] # try - 1
df_list = [df1, df2, df3]
result = pd.concat(df_list, axis=0) # try - 2
# Map your keys sequentially matching the order of dfs
# (df1 joins df2 via id_1 & id_2; df2 joins df3 via id_2 & id_3)
left_keys = [‘id_1’, ‘id_2’]
right_keys = [‘id_2’, ‘id_3’]
def merge_dfs(merged, next_df):
\# Track the current iteration step to pull keys idx = dfs.index(next_df) - 1 return pd.merge(merged, next_df, left_on=left_keys\[idx\], right_on=right_keys\[idx\], how='outer')final_df = reduce(merge_dfs, dfs)
print(final_df)
see error

