Edward Edward - 3 months ago 16
Python Question

Combine two columns of text with NaN in pandas

I want to combine two columns as below

import numpy as np
import pandas as pd
data = pd.DataFrame({ 'a' : [np.nan, 'abc'], 'b' : ['abc', 'abc']})
data['c']=data['a']+' '+data['b']
data

a b c
0 NaN abc NaN
1 abc abc abc abc


The problem is in NaN, i want get

Nan + abc = abc


I can do like this

data = pd.DataFrame({ 'a' : [np.nan, 'abc'], 'b' : ['abc', 'abc']})
data = data.replace( np.nan, '',regex=True)
data['c']=data['a']+' '+data['b']
data

a b c
0 abc abc
1 abc abc abc abc


but it's not always convenient. Are there ways to combine like?

NaN + abc = abc

Answer
>>> data['c']=data['a'].fillna('') + ' ' + data['b'].fillna('')
>>> data
     a    b        c
0  NaN  abc      abc
1  abc  abc  abc abc

However, do note that data['c'][0] == ' abc'. You'd have to use .str.strip() to strip off whitespace if needed.

Comments