Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas combine two strings ignore nan values

I have two columns with strings. I would like to combine them and ignore nan values. Such that:

ColA, Colb, ColA+ColB str   str    strstr str   nan    str nan   str    str 

I tried df['ColA+ColB'] = df['ColA'] + df['ColB'] but that creates a nan value if either column is nan. I've also thought about using concat.

I suppose I could just go with that, and then use some df.ColA+ColB[df[ColA] = nan] = df[ColA] but that seems like quite the workaround.

like image 225
As3adTintin Avatar asked Oct 15 '15 20:10

As3adTintin


People also ask

Does pandas sum ignore NaN?

sum() Method to Find the Sum Ignoring NaN Values. Use the default value of the skipna parameter i.e. skipna=True to find the sum of DataFrame along the specified axis, ignoring NaN values. If you set skipna=True , you'll get NaN values of sums if the DataFrame has NaN values.

How do you replace all NaN values with string in pandas?

Use df. replace(np. nan,'',regex=True) method to replace all NaN values to an empty string in the Pandas DataFrame column.

How do you join two strings in pandas?

By use + operator simply you can concatenate two or multiple text/string columns in pandas DataFrame.


2 Answers

Call fillna and pass an empty str as the fill value and then sum with param axis=1:

In [3]: df = pd.DataFrame({'a':['asd',np.NaN,'asdsa'], 'b':['asdas','asdas',np.NaN]}) df  Out[3]:        a      b 0    asd  asdas 1    NaN  asdas 2  asdsa    NaN  In [7]: df['a+b'] = df.fillna('').sum(axis=1) df  Out[7]:        a      b       a+b 0    asd  asdas  asdasdas 1    NaN  asdas     asdas 2  asdsa    NaN     asdsa 
like image 102
EdChum Avatar answered Sep 22 '22 09:09

EdChum


You could fill the NaN with an empty string:

df['ColA+ColB'] = df['ColA'].fillna('') + df['ColB'].fillna('') 
like image 45
AChampion Avatar answered Sep 22 '22 09:09

AChampion