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.
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'])
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.
df1.set_index('Booking_id', inplace=True)
df2.set_index('ID_Universal', inplace=True)
before_change = 'amount'
after_change = 'Price'
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.