Need help understanding why this alternative answer is not accepted

Screen Link: https://app.dataquest.io/m/285/frequency-distributions/10/readability-for-grouped-frequency-tables

I usually try (and manage :slight_smile: ) to answer the questions while avoiding for loops and df.apply(). This time however, while my method produces an answer that looks identical to the requested output,it is not accepted by the answer checker. Any thoughts as to why this is happening?

My Code:

wnba = pd.read_csv('wnba.csv')
interval = pd.interval_range(start=0, end=600, freq = 60)
gr_freq_table_10 = pd.cut(wnba.PTS, interval).to_frame().groupby("PTS").size()
gr_freq_table_10.index.name = None

What I expected to happen:
The answer to be accepted given it is a series that contains the expected values and index.

What actually happened:
The answer checker doesnโ€™t accept the answer, but happily accepts the answer using the for loop technique

The indexes are different, actually. Try printing both indexes and see what they look like. Donโ€™t just go about what you see when you look at the series object โ€” print("10") and print(10) display indistinguishable output.

The following code should fix your solution.

wnba = pd.read_csv('wnba.csv')
intervals = pd.interval_range(start = 0, end = 600, freq = 60)

gr_freq_table_10 = pd.cut(wnba.PTS, intervals).to_frame().groupby("PTS").size()
gr_freq_table_10.index = pd.Series([0 for _ in range(10)], index = intervals).index

Ah brilliant, thanks! Having gone back and checked pd.cut() docs, they do mention it returns a categorical.
FWIW, inspired by your solution and the fact that pd.interval_range() already returns a IntervaIndex object, I found that this works just as well:

interval = pd.interval_range(start=0, end=600, freq = 60)
gr_freq_table_10 = pd.cut(wnba.PTS, interval).to_frame().groupby("PTS").size()
gr_freq_table_10.index = interval  
2 Likes