How to collapse Pandas Dataframe Columns and Concatenate Strings

一个人想着一个人 提交于 2019-12-05 16:45:52

Maybe:

>>> df.max(axis=1)
1      Car
2      Car
3     Bike
4      Car
5    Train
6    Train
dtype: object

which is a Series, not a DataFrame, but you could make one using df.max(axis=1).to_frame(1) or something.

If they are empty strings rather than NaN you can use .sum:

In [11]: df.fillna('').sum(1)
Out[11]: 
1      Car
2      Car
3     Bike
4      Car
5    Train
6    Train
dtype: object

This would also work, under the assumption there's always a column with a non empty string:

 df.apply(lambda x: [y for y in x.values if y!=''][0],axis=1)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!