Relative frequency from dataframe with 3 columns of raw data?

496 views python
-2

New to Python (& StackOverflow), I am struggling to find a solution to take my ['Product_Name', 'Date_of_Sale', 'Quantity'] data and output the relative frequencies of the daily quantity frequencies per product.

As an example, Product 1 sells 8 units (Day1), 6 units (Day2), 6 (Day 3), 5 (Day 4), 8 (Day 5), 7 (Day 6), 6 (Day 7) over 7 days, giving relative frequencies for Product 1 of {5 units : 0.142, 6 : 0.429, 7 : 0.142, 8 : 0.286}.

How can I do this for all products for a period?

answered question

1 Answer

7

Use value_counts() and to_dict():

import pandas as pd

df = pd.DataFrame({'Day': [1, 2, 3, 4, 5, 6, 7],
    'Product1': [8, 6, 6, 5, 8, 7, 6]})

df['Product1'].value_counts().div(df.shape[0]).to_dict()

Yields:

{6: 0.42857142857142855, 8: 0.2857142857142857, 7: 0.14285714285714285, 5: 0.14285714285714285}

posted this

Have an answer?

JD

Please login first before posting an answer.