[英]Pandas fill empty values on one dataframe from another dataframe
This is something that I would be able to replicate easily in Excel with an XLOOKUP function, but I'm trying to do it with pandas.这是我可以使用 XLOOKUP 函数在 Excel 中轻松复制的内容,但我正在尝试使用 Pandas 来完成。
I have 2 dataframes, say something like this:我有 2 个数据框,可以这样说:
df1 df1
|first_name | last_name | dob | value |
| Goku | Saiyan | 1/1/2021 | |
| Vegetta | Super | 8/7/1990 | |
| Gohan | Son | 4/20/1969| |
df2 df2
|first_name | last_name | dob | value |
| Goku | Saiyan | 1/1/2021 | 50 |
| Vegetta | Super | 8/7/1990 | 92 |
| Gohan | Son | 4/20/1969| 31 |
| Trunks | Donald | 7/1/1921 | 49 |
| New Name | Another | 1/31/1992| 67 |
I would like to fill the value
column in df1 from the value
column in df2.我想,以填补
value
从在DF1列value
在DF2列。
I cannot use combine_first
because the dataframes have different index and different sizes.我不能使用
combine_first
因为数据帧有不同的索引和不同的大小。
If I use pd.merge
then I get the value_x
and value_y
where value_y
has the data that I want, but I need to do more process to have it where I want on df1['value']
如果我使用
pd.merge
然后我得到value_x
和value_y
,其中value_y
有我想要的数据,但我需要做更多的过程才能将它放在我想要的df1['value']
I basically want to match the first name, last name and dob on both dataframes and receive the value
from df2.我基本上想匹配两个数据帧上的名字、姓氏和 dob,并从 df2 接收
value
。
It's probably a simple issue, but I have been struggling with the different methods that I've tried and I think there must be something that I'm missing because it shouldn't be that complicated.这可能是一个简单的问题,但我一直在努力尝试我尝试过的不同方法,我认为一定有我遗漏的东西,因为它不应该那么复杂。
Any help will be really appreciated.任何帮助将不胜感激。
If your value
column from df1
does not contain existing value, you can drop it and use merge
:如果
df1
中的value
列不包含现有值,则可以删除它并使用merge
:
>>> pd.merge(df1.drop(columns='value'), df2, how='left',
on=['first_name', 'last_name', 'dob'])
first_name last_name dob value
0 Goku Saiyan 1/1/2021 50
1 Vegetta Super 8/7/1990 92
2 Gohan Son 4/20/1969 31
Use map.使用地图。
Create a dict of firts_name;创建一个 firts_name 的字典; value from df2 and map to df1's first_name.
df2 的值并映射到 df1 的 first_name。
df1 =df1.assign(value=df1['first_name'].map(dict(zip(df2['first_name'],df2['value']))))
first_name last_name dob value
0 Goku Saiyan 1/1/2021 50
1 Vegetta Super 8/7/1990 92
2 Gohan Son 4/20/1969 31
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.