user4718221
user4718221

Reputation: 606

Pandas dividing the number of aggregated count of dates

I have a Pandas data frame with dates for when each item was listed and sold.

result['dateSold'].value_counts(dropna=False)

2021-03-22       37
2021-03-29       37
2016-07-25       34
2020-11-02       34
2021-03-15       32
2021-02-16       32
2021-03-16       32
2021-03-31       30
2017-05-23       30

I have used the following code to find the number of items sold each month and year.

result['dateSold'].groupby([result.dateSold.dt.year, result.dateSold.dt.month]).agg('count')

dateSold  dateSold
2016.0    4.0         194
          5.0         445
          6.0         463
          7.0         347
          8.0         362
          9.0         324
          10.0        259
          11.0        199
          12.0        174

And the same with dates listed.

result['dateListed'].groupby([result.dateListed.dt.year, result.dateListed.dt.month]).agg('count')

dateListed  dateListed

2017        2                1
            4                1
            5                2
            7                2
            8                1
2018        1                1
            2                1
            3                3
            4                5
            5               14
            6                7
            7                5
            8                2

Now I would like to get a ratio of the number of listed/sold properties for each month and year, and assign it to every row with the corresponding date sold that matches that month and year. I have tried doing it by dividing the Series, but it's not working. What would be the best way to do this?

Upvotes: 0

Views: 38

Answers (1)

jezrael
jezrael

Reputation: 863166

For ratio I think is necessary divide both Series:

s1 = result.groupby([result.dateSold.dt.year, result.dateSold.dt.month])['dateSold'].count()

s2 = result.groupby([result.dateListed.dt.year, result.dateListed.dt.month])['dateListed'].count()

s = s2.div(s1)

Upvotes: 2

Related Questions