Iteratively Concatenate Columns In Pandas With NaN Values
I have a pandas.DataFrame data frame: import pandas as pd df = pd.DataFrame({'x': ['hello there you can go home now', 'why should she care', 'please sort me appropriately'],
Solution 1:
Option 1
pd.Series(df.fillna('').values.tolist()).str.join(' ')
0 hello there you can go home now
1 why should she care finally we were able to go...
2 please sort me appropriately but what about me...
dtype: object
Option 2
df.fillna('').add(' ').sum(1).str.strip()
0 hello there you can go home now
1 why should she care finally we were able to go...
2 please sort me appropriately but what about me...
dtype: object
Solution 2:
Option 3
In [3061]: df.apply(lambda x: x.str.cat(sep=''), axis=1)
Out[3061]:
0 hello there you can go home now
1 why should she carefinally we were able to go ...
2 please sort me appropriatelybut what about mee...
dtype: object
Post a Comment for "Iteratively Concatenate Columns In Pandas With NaN Values"