简体   繁体   English

VLOOKUP 并在 pandas 中一起匹配?

[英]VLOOKUP and match together in pandas?

How can I pass this excel formula to pandas?如何将此 excel 公式传递给 pandas?

=IF(J3="",7,IFERROR(VLOOKUP(I3,Abrang!$A$1:$LT$5956,MATCH(Relatorio!L3,Abrang!$A$3:$LT$3,0),1),0))

How do a merge with this data frames如何与此数据框合并

 data_1 = {'COD(COLUMN_I)': [763807643,45968455,56565435,5833250],
 'Data(COLUMN_J)':["16/11/2021","19/11/2021","19/11/2021","09/11/2021"],
 'Type(COLUMN_L)': ["Type 1", "Type 2","Type 3","Type 4"]}
 
 data_2 = {'COD(COLUMN_I)': [763807643,45968455,56565435,5833250],
          'Type_1':["4","21","9","8"],
          'Type_2': ["5", "45","3","8"],
          'Type_3': ["12", "43","54","6"],
          'Type_4': ["7", "5","2","1"]
          }
 df_1 = pd.DataFrame(data=data_1)
 Abrang = pd.DataFrame(data=data_2)

To get this result?得到这个结果?

在此处输入图像描述

Use melt to reformat your dataframe Abrang then use merge to lookup the right rows:使用melt重新格式化您的 dataframe Abrang然后使用merge查找正确的行:

df_2 = Abrang.melt('COD(COLUMN_I)', var_name='Type(COLUMN_L)', value_name='Result')
df_2['Type(COLUMN_L)'] = df_2['Type(COLUMN_L)'].str.replace('_', ' ')

out = df_1.merge(df_2, on=['COD(COLUMN_I)', 'Type(COLUMN_L)'], how='left')

Output: Output:

>>> out
   COD(COLUMN_I) Data(COLUMN_J) Type(COLUMN_L) Result
0      763807643     16/11/2021         Type 1      4
1       45968455     19/11/2021         Type 2     45
2       56565435     19/11/2021         Type 3     54
3        5833250     09/11/2021         Type 4      1

Note : the code could be simpler if the Type column/value was the same between the two dataframes: 'Type 1' and 'Type_1', etc.注意:如果两个数据帧之间的Type列/值相同,代码可能会更简单:'Type 1'和'Type_1'等。

The docs has an example that you can adapt;该文档有一个您可以修改的示例 however, you should reshape the last column to match Abrang :但是,您应该重塑最后一列以匹配Abrang

df_1 = df_1.assign(Result = df_1.iloc[:, -1].str.split().str.join('_'))

idx, cols = pd.factorize(df_1.Result)

df_1 = df_1.assign(Result = Abrang
                            .reindex(cols, axis=1)
                            .to_numpy()[np.arange(len(df_1)), idx]


   COD(COLUMN_I) Data(COLUMN_J) Type(COLUMN_L) Result
0      763807643     16/11/2021         Type 1      4
1       45968455     19/11/2021         Type 2     45
2       56565435     19/11/2021         Type 3     54
3        5833250     09/11/2021         Type 4      1

See here for more ways to handle lookup有关处理查找的更多方法,请参见此处

Another option is with a pivot, but is considerably longer:另一种选择是使用 pivot,但要长得多:

A, B = df_1.iloc[:, 0], df_1.iloc[:, -1]
B.index = A
Abrang['Result'] = Abrang.iloc[:, 0].map(B)
cols = [*zip(Abrang.columns[1:], B)]
result = (Abrang
          .pivot(Abrang.columns[0], 'Result')
          .loc[:, cols]
          .ffill(axis=1)
          .iloc[:, -1]
          )

df_1.assign(Result = df_1.iloc[:, 0].map(result))

   COD(COLUMN_I) Data(COLUMN_J) Type(COLUMN_L) Result
0      763807643     16/11/2021         Type 1      4
1       45968455     19/11/2021         Type 2     45
2       56565435     19/11/2021         Type 3     54
3        5833250     09/11/2021         Type 4      1

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

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