My dataset df looks like this:
time high low offset
2017-01-01 1.012 0.921 NaN
2017-01-02 1.019 0.934 NaN
2017-01-03 1.213 NaN 0.982
2017-01-04 1.112 0.965 NaN
here, either low value is NaN or offset value is NaN but not both
I want to create a new column called low_offset_merge
so that low_offset_merge will contain the merged values of low and offset and it will all have a number value and no NaN, like below:
time high low offset low_offset_merge
2017-01-01 1.012 0.921 NaN 0.921
2017-01-02 1.019 0.934 NaN 0.934
2017-01-03 1.213 NaN 0.982 0.982
2017-01-04 1.112 0.965 NaN 0.965
What did I do?
- First, check if `low` has `NaN` values and set the value
df.loc[df['low'] != 'NaN', 'low_offset_merge'] = df['offset']
- Second, fill in the `NaN` column
df['low_offset_merge'] = df['low_offset_merge'].fillna(value=df['offset'])
I am looking for a one-line solution, can you please help?
You were close with your last attempt using fillna. You don't need to check for NaN values first, you can just directly fill the missing values with values from another column:
df['low_offset_merge'] = df['low'].fillna(df['offset'])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With