import pandas as pd
df_1 = pd.DataFrame(columns = ['A', 'B'])
df_1.loc[len(df_1)] = [5,6]
df_1.loc[len(df_1)] = [8, 1]
df_1.loc[len(df_1)] = [6, 7]
df_1.loc[len(df_1)] = [4, 9]
df_1.loc[len(df_1)] = [1, 3]
df_1.loc[len(df_1)] = [9, 2]
df_1.loc[len(df_1)] = [2, 5]
df_2 = pd.DataFrame(columns = ['A', 'B', 'A_bin', 'B_bin'])
df_2.loc[len(df_2)] = [5, 6, 2, 2]
df_2.loc[len(df_2)] = [8, 1, 1, 1]
df_2.loc[len(df_2)] = [6, 7, 3, 2]
df_2.loc[len(df_2)] = [4, 9, 3, 3]
df_2.loc[len(df_2)] = [1, 3, 1, 1]
df_2.loc[len(df_2)] = [9, 2, 1, 1]
df_2.loc[len(df_2)] = [2, 5, 2, 2]
df_3 = pd.DataFrame(columns = ['A', 'B', 'C', 'D', 'A_bin', 'B_bin', 'C_bin', 'D_bin'])
df_3.loc[len(df_3)] = [5, 6, 2, 6, 2, 2, 1, 2]
df_3.loc[len(df_3)] = [8, 1, 6, 4, 1, 1, 2, 2]
df_3.loc[len(df_3)] = [6, 7, 3, 1, 3, 2, 1, 1]
df_3.loc[len(df_3)] = [4, 9, 1, 9, 3, 3, 1, 3]
df_3.loc[len(df_3)] = [1, 3, 8, 7, 1, 1, 3, 3]
df_3.loc[len(df_3)] = [9, 2, 4, 8, 1, 1, 2, 3]
df_3.loc[len(df_3)] = [2, 5, 9, 2, 2, 2, 3, 1]
results = {}
df_1_columns = list(df_1.columns)
df_3_op_columns = [cname for cname in list(df_3.columns) if not cname.endswith("_bin")]
for bin_name in (column_name + "_bin" for column_name in df_1_columns):
df_3_joined = pd.merge(df_3[df_3_op_columns], df_2[df_2[bin_name] == 2][df_1_columns], how='right', on=df_1_columns)
results[bin_name] = df_3_joined
for binName, result in results.iteritems():
print(binName)
print(result)