I have a dataframe (df1) that contains information about project_id, cost_center and other features:
| project_id | cost_center | month |
|---|---|---|
| 101 | 8575 | 3 |
| 321 | 8597 | 4 |
| 321 | 8597 | 2 |
| Nan | 8522 | 1 |
Sometimes the project_id is not included there, so is Nan, and for these cases I have a "mapping table" (df2) that indicates the project_id that should be associated to that cost center:
| project_id | cost_center |
|---|---|
| 832 | 8522 |
So in my example, I should be able to replace the Nan in df1 for a 832. It means, I should replace the project_id in df1, when the cost_center is in df2.
I tried the following code, but is not working. It says "Length of values (0) does not match length of index (565)" I think because df1 and df2 have different sizes
df['project_id'] = df_mapping[df['cost_center'].isin(df_mapping['cost_center'])]['project_id'].values
df1['project'_id'] = df1['project'_id'].fillna(df1['cost_center'].map(df2.set_index('cost_center')['project_id']))