[英]How i can filter a dataframe with another dataframe column, both have different index and few value in second dataframe?
[英]How to use apply and lambda to set value to a dataframe column based on the index matching another column in a second dataframe
我有以下兩個dataframe
df1:
item_id height weight
13 19902 1.56 54
28 20503 1.7 30
df2:
height_2 weight_2 size-> (not supposed to be modified)
item_id
19902 1 50 8
20503 2 30 10
expected output:
df2:
height_2 weight_2 size-> (not supposed to be modified)
item_id
19902 1.56 54 8
20503 1.7 30 10
我想用 d1 中的值替換 d2 中的身高和體重。 通常的方法是循環 d2 並為每一行獲取索引並使用索引與 d1 匹配以獲取相應的身高和體重寫入 d2。 這很慢。 任何人都知道如何在熊貓 dataframe 中使用更快的技術,也許應用 function? 我嘗試了以下方法,但它不起作用:
df2['height'] = df2.apply(lambda x: df1.loc[(df1.item_id == x.name),['height']])
引發以下錯誤:
> ValueError: If using all scalar values, you must pass an index
使用DataFrame.update
並通過DataFrame.set_index
將item_id
轉換為索引,對於正確匹配的列名也使用字典rename
:
d = {'height':'height_2','weight':'weight_2'}
df2.update(df1.set_index('item_id').rename(columns=d))
print (df2)
height_2 weight_2 size
item_id
19902 1.56 54 8
20503 1.70 30 10
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.