Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Concatenating two Pandas columns based on a condition

I have two columns and I’m trying to make a new one depending if one has a null value or not. I have the following example and I´m trying to use the np.where() function but it doesn´t seems to work.

import pandas as pd 
import numpy as np 

# DF 1
a = pd.DataFrame([1,'nan',2],columns=['a1'])
# DF 2
b = pd.DataFrame(['hola','hola','hola'],columns=['b1']) 
# New Column
b['b2'] = np.where(a['a1'].astype(str) != 'nan', b['b1'] + a['a1'].astype(str)) 

The result for the new column ‘b2’ should be:

hola1  
hola 
hola2

The np.where function also doesn´t has like an else option so I don´t know how to include that. I appreciate the help!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need to "synchronize" a and b dataframes to make pairwise comparisons (possibly with pd.concat):

b['b2'] = pd.concat([a.replace({'nan': ''}), b], axis=1).apply(lambda x:x['b1'] + str(x['a1']), axis=1)
print(b)

     b1     b2
0  hola  hola1
1  hola   hola
2  hola  hola2
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading