3 Upvotes

Pivoting Pandas Dataframes

Python
Data Preprocessing

Creates a new DataFrame that pivots the orders Dataframe so the rows are product IDs, the columns are order dates and the cell values are units sold. Any NAs are filled with 0s.

units_by_day = orders.groupby(['Order_Date','ProductID'],as_index=False)['units'].sum()
units_by_day_pivot = units_by_day.pivot(index='ProductID', columns='Order_Date', values='units').reset_index()
units_by_day_pivot.fillna(0, inplace=True)

By analyseup - Last Updated Jan. 12, 2022, 12:23 a.m.

Did you find this snippet useful?

Sign up to bookmark this in your snippet library

COMMENTS
RELATED SNIPPETS
2
Top Contributors
98