[Python] How should we take the sum of values in a column after grouping by a different column...

Discussão em 'Python' iniciado por Stack, Julho 22, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to plot a graph for to analyze if there's any relation between the available_days of a property and number of reviews for it. I have a dataset which has different unique property listings, available_days for each property, number of reviews for each property. I am trying to plot by grouping the data by 'available_days' and I need to count the total number of reviews for those properties. For example, if the available days are 25, then I need to take the sum of the number of reviews for all properties with 25 available days. I couldn't figure out a way to do this. I tried as below but it is not giving me the expected result.

    available_days=listings.groupby(['availability_365']).count()
    available_days=listings.groupby(['availability_365'])['reviews_count'].count()
    available_days=listings.groupby('availability_365').agg('sum')
    available_days=listings.groupby(['availability_365']).agg({'reviews_count':np.sum})


    Here is the dataset I am referring to:[​IMG]

    This is the desired output format: [​IMG]

    Also, please suggest a better way of approaching this problem to plot the graph.

    Continue reading...

Compartilhe esta Página