Skip to content

ENH: df.implode as a opposite function to df.explodeΒ #45459

@jlynchMicron

Description

@jlynchMicron

Is your feature request related to a problem?

This seems to be kind of a niche issue, but I found myself needing to do this operation to format data in a way that an SQL table ARRAY structure would accept it using the pandas.to_sql function (in conjunction with sqlalchemy).

Describe the solution you'd like

The set of operations needed is described in this stackoverflow post: https://stackoverflow.com/questions/64235312/how-to-implodereverse-of-pandas-explode-based-on-a-column

Example:

df:
  NETWORK       config_id       APPLICABLE_DAYS
0   Grocery     5399            SUN
1   Grocery     5399            MON
2   Grocery     5399            TUE
3   Grocery     5399            WED

df.implode('APPLICABLE_DAYS')
   NETWORK  config_id       APPLICABLE_DAYS 
0  Grocery       5399       SUN,MON,TUE,WED

The set of operations actual used were the following:

df = (df.groupby(['NETWORK','config_id'])
      .agg({'APPLICABLE_DAYS': lambda x: ",".join(x)})
      .reset_index())

Metadata

Metadata

Assignees

No one assigned

    Labels

    EnhancementReshapingConcat, Merge/Join, Stack/Unstack, Explode

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions