![](/img/trans.png)
[英]Use a value from one dataframe to lookup the value in another and return an adjacent cell value and update the first dataframe value
[英]Python-Lookup a cell value in another df, return a boolean value to another cell
我是 Python 編碼的大三學生。 我有一個項目使用 Python 代碼對 excel 文件進行排序。 有 3 個條件進行排序,值需要以任何方式編碼才能排序。 以下是我正在處理的 2 個數據框。 如果在第二個 dataframe 中找到,我需要用 boolean 值填寫“折扣供應商(真或假)”列。
[在此處輸入圖片描述][1] [在此處輸入圖片描述][2]
我到處搜索,看到很多關於 vlookup 使用 pandas.merge、pd.assign 等的答案。我嘗試了很多類型,但無法合並到我真正的 excel 文件中。 這是我第一次在這個網站上寫文章。 如果有不清楚的信息使您無法回答我的問題,請回復以便我提供更多信息。 如果您能提供幫助,我們將不勝感激:Van [1]: https://i.stack.imgur.com/0tya8.png [2]: https://i.stack.imgur.com/liTrc.png
我使用嵌套迭代編寫了這段代碼。 有沒有更好的方法來使用不同的方法對這些語句進行編碼? 我的代碼如下。
for i in range(len(df1)):
for j in range(len(df2)):
if df1.at[i,'Vendor Name']==df2.at[j,'Discount Vendor List']:
df1.at[i,'Discount Vendor(True/False)']='True'
else:
df1.at[i,'Discount Vendor(True/False)']='False'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.