CodexBloom - Programming Q&A Platform

Pandas DataFrame not grouping correctly when using custom lambda function with multiple columns

👀 Views: 56 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-10
pandas dataframe groupby python

I'm working with an scenario while trying to group a Pandas DataFrame using a custom lambda function that takes into account multiple columns... I want to group by two columns, 'category' and 'sub_category', and then calculate the average of a 'value' column based on this grouping. However, the output seems to be incorrect. Here is a simplified version of my code: ```python import pandas as pd # Sample DataFrame data = { 'category': ['A', 'A', 'B', 'B', 'A'], 'sub_category': ['X', 'Y', 'X', 'Y', 'Y'], 'value': [1, 2, 3, 4, 5] } df = pd.DataFrame(data) # Attempting to group by 'category' and 'sub_category' result = df.groupby(['category', 'sub_category']).agg(lambda x: x.mean()).reset_index() print(result) ``` The expected output should give me the averages for each combination of 'category' and 'sub_category', like so: ``` category sub_category value 0 A X 1.0 1 A Y 3.5 2 B X 3.0 3 B Y 4.0 ``` However, when I run this code, I get a TypeError: ``` TypeError: 'function' object is not subscriptable ``` I suspect this might be because of how I'm using the lambda function for aggregation. I've tried changing the lambda to a named function, but that hasn't resolved the scenario either. Can anyone guide to understand why this is happening and how to fix it? I'm using Pandas version 1.3.3. Thanks in advance! I've been using Python for about a year now.