提问人:SooHana 提问时间:11/9/2023 最后编辑:SooHana 更新时间:11/14/2023 访问量:70
如何将excel中的vlookup函数更改为python脚本,如图所示?
How to change the vlookup function in excel into python script as the picture shown?
问:
我想将 vlookup 函数转换为 python 脚本,但我真的不知道如何制作。附上的图片将是我在 excel 应用程序中应用的 vlookup 功能。lookup_value将是 excel A 中的列名“申报上的 HS 代码”,而table_array将是 excel B 中的 A 到 B 列。有人可以帮我弄清楚如何将其转换为 python 脚本吗?
答:
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,下次会避免它。谢谢你提醒我!
评论