简体   繁体   English

熊猫从另一个数据帧填充一个数据帧上的空值

[英]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_xvalue_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.

相关问题 熊猫用另一个数据框范围内的值计数填充数据框 - Pandas fill dataframe with count of values within a range from another dataframe 如何使用不同的 datetimeindex 将 pandas dataframe 中的值填充到另一个 dataframe - How to fill values from a pandas dataframe to another dataframe with different datetimeindex Pandas 从另一个 dataframe 填充 dataframe 中的缺失值 - Pandas fill missing values in dataframe from another dataframe 用另一个值填充一个 dataframe - fill one dataframe with values ​from another 从另一个大小的数据框中一一填充数据框中的空值 - Fill null values in a dataframe from another sized dataframe one by one 如何使用来自另一个数据帧(熊猫)的值更新空的 dataframe 值? - How to update the empty dataframe values with values from another dataframe(Pandas)? 用熊猫数据框中另一列的相同值填充空值 - fill up empty values with same value of another column in pandas dataframe 根据两者的索引将一个数据帧中的值填充到另一个数据帧中 - Fill values from one dataframe into another dataframe based on index of the two 如何将特定值从一个 dataframe 填充到另一个 dataframe - How to fill specific values from one dataframe to another dataframe 用另一个数据框中的值替换一个熊猫数据框中的值 - Replacing values in one pandas dataframe with values from another dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM