Search code examples
pythonpandasdataframereshapemelt

How to reshape dataframe with pandas?


I have a data frame that contains product sales for each day starting from 2018 to 2021 year. Dataframe contains four columns (Date, Place, Product Category and Sales). From the first two columns (Date, Place) I want to use the available data to fill in the gaps. Once the data is added, I would like to delete rows that do not have data in ProductCategory. I would like to do in python pandas.

The sample of my data set looked like this:

My Data frame

I would like the dataframe to look like this:

output dataframe


Solution

  • Use fillna with method 'ffill' that propagates last valid observation forward to next valid backfill. Then drop the rows that contain NAs.

    df['Date'].fillna(method='ffill',inplace=True)
    df['Place'].fillna(method='ffill',inplace=True)
    df.dropna(inplace=True)