pythonpandas

Opposite operation for explode in pandas for several columns at once


I have a pandas dataframe with 150 columns and 5000 rows. I want to select only those rows with a given condition, i.e. df[df['first_column] == 1]. Then, I want to convert all the columns of this selection to a single row, where each column is a list. i.e.:

print(df[df['first_column] == 1])
first_column second_column third_column ...
1            2             A
1            4             B
1            6             A

I want this to be:

first_column second_column third_column ...
1            2, 4, 6       A, B, A

where in each column either is a list or a dataframe. Which approach should I use to avoid for loops?


Solution

  • Simple check

    df.groupby('first_column').agg(','.join)