The addition of chains of pandas through columns

advertisements

This is perfectly legal in Python:

In [1]: 'abc' + 'def'
Out[1]: 'abcdef'

If I have an all text Pandas DataFrame, like the example below:

In [2]: df = pd.DataFrame([list('abcd'), list('efgh'), list('ijkl')],
                          columns=['C1','C2','C3','C4'])
        df.loc[[0,2], ['C2', 'C3']] = np.nan
        df
Out[2]:     C1  C2  C3  C4
        0   a   NaN NaN d
        1   e   f   g   h
        2   i   NaN NaN l

Is it possible to do the same with columns of the above DataFrame? Something like:

In [3]: df.apply(+, axis=1) # Or
        df.sum(axis=1)

Note that both of the statements above don't work. Using .str.cat() in a loop is easy, but I am looking for something better.


Expected output is:

Out[3]:    C
        0  ad
        1  efgh
        2  il


You could do

df.fillna('').sum(axis=1)

Of course, this assumes that your dataframe is made up only of strings and NaNs.