I am attempting to merge two columns which contain strings and nans. When I attempt to merge them I cannot deal with the nan values.
df['col3] = df['col1'] + df['col2']
returns only my col2 values
df['col3'] = df['col1'].map(str) + df['col2'].map(str)
returns my nans attached to each other.
If I don't use .map(str) then the .nan values don't concatenate at all.
Is there a way to concatenate two dataframe columns so that if either of them are nan they aren't concatenated. Unless both are nan in which case I do want the return nan.
Example:
df
col0 col1 col2 col3
X A nan A
Y nan B B
Z nan nan nan
W '' B B
You could index first on the last two columns and ffill:
df['col3'] = df[['col1', 'col2']].ffill(1).col2
col0 col1 col2 col3
0 X A NaN A
1 Y NaN B B
2 Z NaN NaN NaN
3 W '' B B
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