Search code examples
python-3.xpandasdataframe

Plotting the count of values per week from a Dataframe


Say I have a pandas dataframe of the following format illustrating the total purchase value ($) of a person in a store at some time stamp for everyday in the year of 2015:

       person_num               date       value
0             1  2015-01-01 00:23:32       298.00
1             2  2015-01-01 00:31:59       348.00
2             3  2015-01-01 02:30:21       533.00
3             4  2015-01-01 03:14:10       022.00
4             5  2015-01-01 05:51:43       448.00
5             6  2015-01-01 07:10:08       158.00
...         ...                  ...          ...
123728   123729  2015-12-31 23:13:34       109.00

etc. and this goes up to 2015-12-31... How would I be able to plot the total value per week. So the plot would have the week number on the x-axis and total value (sum of all the purchase amounts per week ) on y-axis.

Edit: This pertains only to the value column not to the person num


Solution

  • import pandas as pd
    
    # Construct input dataframe
    df = pd.DataFrame({
        "person_num": [
            1,
            2,
            3,
            4,
            5,
            6,
        ],
        "date": [
            "2015-01-01 00:23:32",
            "2015-01-02 00:31:59",
            "2015-01-05 02:30:21",
            "2015-01-06 03:14:10",
            "2015-01-15 05:51:43",
            "2015-01-16 07:10:08",
        ],
        "value": [
            298.00,
            348.00,
            533.00,
            022.00,
            448.00,
            158.00,
        ],
    })
    
    # Construct dataframe with week number column and values column
    df2 = df.loc[:, ["value"]]
    df2["week_no"] = pd.to_datetime(df["date"]).dt.week
    
    # Sum up weekly values
    df3 = (
        df2
        .groupby("week_no")
        .sum()
        .reset_index()
    )
    
    # Plot results
    df3.plot.scatter(x="week_no", y="value")