首页 > 解决方案 > Assigning dataframe id based on id in another dataframe

问题描述

I have two dataframes called dataframe A and dataframe B

             A
    id| column1 |column2
    3 |    439  |  2398
    41|    498  |   34
    2 |    233  |   43

           B
    column1 |column2
      439   |  2398
      498   |   56
      233   |   43

I would like to assign an id column to dataframe B based on the ids in dataframe A. If a row is present in dataframe A and in dataframe B, I would like to assign the corresponding id from dataframe A to dataframe B. If a row in dataframe B is not in dataframe A I would like to assign "None" to that cell in the id column in B. That is, I would like to have

             B
    id  | column1 |column2
    3   |    439  |  2398
    None|    498  |   56
    2   |    233  |   43

Question: How do I assign the ids from dataframe A to dataframe B based on the columns in dataframe B?

标签: pythonpython-3.xpandasdataframe

解决方案


You can use a left merge. Note for a numeric series NaN values will force id series to become of type float. In most situations this isn't a problem, and I wouldn't advise you to "force" type conversion row-by-row.

res = pd.merge(B, A, how='left')

print(res)

   column1  column2   id
0      439     2398  3.0
1      498       56  NaN
2      233       43  2.0

推荐阅读