Home > Enterprise >  Combine Pandas df rows into one
Combine Pandas df rows into one

Time:02-25

For every two rows in my df, I would like to concatenate them into one.

Starting with this:

Original

and ending with this:

desired

I've been able to apply this to one column, but have not been able to apply it across all of them. I would also like to loop this for every two rows for the entire df.

This is my actual df:

                       Team         Spread 
0                Wagner Seahawks  (-11.5, -118)
1    Fairleigh Dickinson Knights   (11.5, -110)

I know this isn't the best way to format a table, but for my needs it is the best option. Thank you

If I were to do this in excel - I would use this:

=TEXTJOIN(CHAR(10),TRUE,A1:A2)

CodePudding user response:

Does this work for you?

>>> df = pd.DataFrame({
        "Col1": ["A", "B", "C", "D"],
        "Col2": [(-11.5, -118), (11.5, -110), (-11.5, -118), (11.5, -110)],
    })
>>> df
  Col1           Col2
0    A  (-11.5, -118)
1    B   (11.5, -110)
2    C  (-11.5, -118)
3    D   (11.5, -110)

If you have non-string columns, you'll need to transform them to string first:

>>> df["Col2"] = df["Col2"].astype(str)

Now, use .groupby using real floor division, and aggregate each pair of rows using "\n".join.

>>> df = df.groupby(df.index // 2).agg("\n".join)
>>> df
   Col1                         Col2
0  A\nB  (-11.5, -118)\n(11.5, -110)
1  C\nD  (-11.5, -118)\n(11.5, -110)

Consider that you would need to write the Excel file on your own to dump the dataframe and load the Excel in the format that you want (as described in this SO answer).

  • Related