繁体   English   中英

从 2 个 DataFrame 合并到 1 个

[英]Consolidation from 2 DataFrames to 1

我写了下面的代码。 它有效,但我相信我可以做得更清晰、更快。

这个想法是:

  • 我有 2 个输入数据帧,我想要 1 DataFrame 作为 output。
  • 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM