如何将excel中的vlookup函数更改为python脚本,如图所示?

How to change the vlookup function in excel into python script as the picture shown?

提问人:SooHana 提问时间:11/9/2023 最后编辑:SooHana 更新时间:11/14/2023 访问量:70

问:

我想将 vlookup 函数转换为 python 脚本,但我真的不知道如何制作。附上的图片将是我在 excel 应用程序中应用的 vlookup 功能。lookup_value将是 excel A 中的列名“申报上的 HS 代码”,而table_array将是 excel B 中的 A 到 B 列。有人可以帮我弄清楚如何将其转换为 python 脚本吗?enter image description here

vlookup 函数将应用于 HS CODE 2017 列:enter image description here

应用vlookup后,HS CODE 2017下的值将为:enter image description here

这是excel B的屏幕截图,其中将从A列到B列选择表格数组:enter image description here

python excel 机器人框架 vlookup

评论

0赞 Lutz 11/9/2023
您的表是否有任何标题?
0赞 user_stack_overflow 11/9/2023
这怎么可能?您将需要更多参数,例如,excel文件名,其位置等ctera?详细说明您想要实现的目标
0赞 SooHana 11/9/2023
@Lutz是的,我的表有标题名称
0赞 SooHana 11/9/2023
@NikhilS 嗨,第一个 excel 路径将完成.xlsx,而第二个将是 CORRELATION TABLE_EXCEL_HS 2022-2017.xlsx,从 excel A 获取的查找值,table_array从 excel b 获取

答:

0赞 SooHana 11/14/2023 #1

我找到了解决方案:

import pandas as pd

def vlookup(excel_a_path, excel_b_path):
# Load Excel files into pandas dataframes
df_a = pd.read_excel(excel_a_path)
df_b = pd.read_excel(excel_b_path)

# Define the columns for lookup
lookup_column = 'HS code on declaration'
table_array_columns = ['PDK2022', 'PDK2017']
range_lookup = False  # Use exact match, equivalent to 0 in Excel

# Perform VLOOKUP
result_df = pd.merge(df_a[[lookup_column]], df_b[table_array_columns], how='left', left_on=lookup_column, right_on=table_array_columns[0])

# Extract the result column as a list
result_column = result_df[table_array_columns[1]].tolist()

return result_column

# Example usage:
excel_a_path = 'path_to_excel_A.xlsx'
excel_b_path = 'path_to_excel_B.xlsx'
result_list = vlookup(excel_a_path, excel_b_path)
print(result_list)

评论

1赞 Chris 11/14/2023
您是否知道我们的政策禁止 AI 生成的答案
0赞 SooHana 11/14/2023
哦,我不知道这个政策,因为很少使用 stackoverflow,下次会避免它。谢谢你提醒我!