Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to collapse Pandas Dataframe Columns and Concatenate Strings

I have a Data Frame df0 with n columns. Only one of the columns contains a string, all other columns are empty or contain the "" string.

Is it possible to collapse the data frame into a single column data frame where for each row I get the non-empty element?

df0:

    A    B     C
1  Car
2  Car 
3       Bike
4  Car
5            Train
6            Train

should give:

    1    
1  Car
2  Car 
3  Bike
4  Car
5  Train
6  Train
like image 797
prre72 Avatar asked Dec 19 '22 16:12

prre72


2 Answers

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.

like image 66
DSM Avatar answered Apr 23 '23 07:04

DSM


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
like image 36
Andy Hayden Avatar answered Apr 23 '23 06:04

Andy Hayden