我已经编写了下面的代码。它有效,但我相信我可以做得更清晰、更快。
想法是:
- 我有2个输入DataFrame,我想要1个DataFrame作为输出
- DF1类似于Name、Attribute1、Attribute2、Attribute3
- DF2类似于Name1、Name2、Value1、Value2
我希望,对于DF2的每一行,NameX都被DF1中的属性列表所取代。
import pandas as pd
# dictionary 1
dico_1 = {
'Name': ['A', 'B', 'C'],
'Attr1': ['XXX', 'YYY', 'XXX'],
'Attr2': ['YYY', 'ZZZ', 'YYY'],
}
dico_2 = {
'Pair_1': ['A', 'B', 'B', 'A'],
'Pair_2': ['B', 'C', 'A', 'C'],
'V1': ['V1_AB', 'V1_BC', 'V1_BA', 'V1_AC'],
'V2': ['V2_AB', 'V2_BC', 'V2_BA', 'V2_AC']
}
df1 = pd.DataFrame(dico_1)
df2 = pd.DataFrame(dico_2)
def cons(df1, df2, row):
P1 = df2['Pair_1'][row]
P2 = df2['Pair_2'][row]
tmp1 = df1.loc[df1['Name'] == P1, "Attr1":"Attr2"]
tmp2 = df1.loc[df1['Name'] == P2, "Attr1":"Attr2"]
tmp3 = pd.DataFrame(df2.loc[row, "V1":"V2"]).transpose()
tmp1.reset_index(drop=True, inplace=True)
tmp2.reset_index(drop=True, inplace=True)
tmp3.reset_index(drop=True, inplace=True)
tmp1 = tmp1.add_suffix('_Pair1')
tmp2 = tmp2.add_suffix('_Pair2')
a = pd.concat([tmp1, tmp2, tmp3], axis=1)
return a
df3 = pd.DataFrame(index=range(df2.shape[0]),
columns=['Attr1_Pair1', 'Attr2_Pair1', 'Attr1_Pair2', 'Attr2_Pair2', 'V1', 'V2'])
for row in range(df2.shape[0]):
line = cons(df1, df2, row)
df3.loc[row] = line.iloc[0]
df3
让我们尝试两个合并:
import pandas as pd
dico_1 = {'Name': ['A', 'B', 'C'], 'Attr1': ['XXX', 'YYY', 'XXX'],
'Attr2': ['YYY', 'ZZZ', 'YYY'], }
dico_2 = {'Pair_1': ['A', 'B', 'B', 'A'], 'Pair_2': ['B', 'C', 'A', 'C'],
'V1': ['V1_AB', 'V1_BC', 'V1_BA', 'V1_AC'],
'V2': ['V2_AB', 'V2_BC', 'V2_BA', 'V2_AC']}
df1 = pd.DataFrame(dico_1)
df2 = pd.DataFrame(dico_2)
# Merge with DF1 on Pair_1 then Merge again with DF1 on Pair_2
df3 = df2.merge(df1, left_on='Pair_1', right_on='Name')
.merge(df1, left_on='Pair_2', right_on='Name',
suffixes=('_Pair1', '_Pair2'))
# Drop Extra Columns
df3 = df3.drop(columns=['Name_Pair1', 'Name_Pair2', 'Pair_1', 'Pair_2'])
print(df3)
df3:
V1 V2 Attr1_Pair1 Attr2_Pair1 Attr1_Pair2 Attr2_Pair2
0 V1_AB V2_AB XXX YYY YYY ZZZ
1 V1_BC V2_BC YYY ZZZ XXX YYY
2 V1_BA V2_BA YYY ZZZ XXX YYY
3 V1_AC V2_AC XXX YYY XXX YYY