how can i rearrange a panda dataframe into this specific configuration?

I think you need DataFrame.melt with GroupBy.size if need counts values per 3 columns:

df1 = df.melt(id_vars=['CentroidID_O', 'CentroidID_D'], var_name='dt_15')
df2 = (df1.groupby(['CentroidID_O', 'CentroidID_D', 'dt_15'])
          .size()
          .reset_index(name='counts'))

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top