AnalystPath

Weighted Average Rating

PandasEasyJunior level~10 min

Problem

You are given a DataFrame `rating_bucket` with columns `bucket_id`, `stars`, and `respondents`. `bucket_id` is unique. Instead of one row per survey response, the data is compressed: each row says that `respondents` people all gave the same `stars` rating.

Compute the average rating across all individual respondents, rounded to 2 decimal places, and return it in a single column named `avg_rating`.

Input data

Example rows — the live problem includes the full dataset.

rating_bucket
bucket_idstarsrespondents
1510
246
333
411

Expected output

Your answer should return 1 row with the columns avg_rating.

Starter code (Pandas (Python))

import pandas as pd

def weighted_average_rating(rating_bucket) -> pd.DataFrame:
    # Your code here
    return rating_bucket

Solve this Pandas question free

Write Pandas (Python) and run it instantly in your browser — even on your phone. No signup needed to try.

Solution & explanation

Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.

Sign up free to unlock

Related Pandas questions