Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does NaN interfere with column concatenation in pandas?

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
like image 344
Violatic Avatar asked Dec 01 '25 02:12

Violatic


1 Answers

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
like image 182
yatu Avatar answered Dec 04 '25 07:12

yatu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!