Home > Software design >  How to collapse all rows in pandas dataframe across all columns
How to collapse all rows in pandas dataframe across all columns

Time:02-23

I am trying to collapse all the rows of a dataframe into one single row across all columns.

My data frame looks like the following:

name job value
bob business 100
NAN dentist Nan
jack Nan Nan

I am trying to get the following output:

name job value
bob jack business dentist 100

I am trying to group across all columns, I do not care if the value column is converted to dtype object (string).

I'm just trying to collapse all the rows across all columns.

I've tried groupby(index=0) but did not get good results.

CodePudding user response:

You could apply join:

out = df.apply(lambda x: ' '.join(x.dropna().astype(str))).to_frame().T

Output:

       name               job  value
0  bob jack  business dentist  100.0

CodePudding user response:

Try this:

new_df = df.agg(lambda x: x.dropna().astype(str).tolist()).str.join(' ').to_frame().T

Output:

>>> new_df
       name               job  value
0  bob jack  business dentist  100.0
  • Related