Home > Software design >  How to copy a column in Pandas from one dataset to another based on condition
How to copy a column in Pandas from one dataset to another based on condition

Time:01-24

I have two datasets in this form:

First Dataset:

Booking_id   Booking amount   
6678           576545
6429           654556
6452           755849
6056           358749
6084           NaN
6542           768087

Second Dataset:

ID_Universal   Price
4857           576545
6084           465488
4886           755849
3868           358749
8669           765889
4684           768087

In First dataset there are few NaN values of Booking_amount... But they are present in dataset 2. For example, if you will see id=6084 has Nan value in dataset 1 but for the same id, the amount value is present in dataset 2. Simillarly there are multiple Nan values for Booking_amount in dataset 1 that are present in dataset 2. So, I want to copy the values from dataset 2 to dataset 1 where dataset 1 has Nan values but for the same id, it's value is present in dataset 2.

CodePudding user response:

fillna can help you, if ID_Universal is unique in your 2nd dataframe.

first_df.set_index('Booking_id')['Booking_amount'].fillna(second_df.set_index('ID_Universal')['Price'])

CodePudding user response:

First you have to set index for both dataframe that are uniquely defined

Let say First Dataset has Booking_id and Second Dataset has ID_Universal that are unique and same.


First set these columns as index

df1.set_index('Booking_id', inplace=True)
df2.set_index('ID_Universal', inplace=True)

Second Make variable based on both dataframe columns name based on which you have to fill and from which you have to fill.
before_change = 'amount'
after_change = 'Price'

Making column name same and applying fillna to fill that nan values and then in final step replacing those column name back to original.
df1.rename(columns={before_change:after_change}, inplace=True)

df1.fillna(df2, inplace=True)

df1.rename(columns={after_change:before_change}, inplace=True)
  •  Tags:  
  • Related