CodexBloom - Programming Q&A Platform

Pandas - implementing GroupBy and Aggregating Custom Functions on DataFrames with Mixed Data Types

👀 Views: 70 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-12
pandas groupby aggregation Python

I'm working with an scenario while trying to use `groupby` combined with a custom aggregation function on a DataFrame that contains mixed data types. My DataFrame has a datetime column, a numeric column, and a string column, and I'm attempting to group by the datetime field and then aggregate the numeric column with a custom sum function, while concatenating the string column into a list. Here's a small snippet of my DataFrame: ```python import pandas as pd from datetime import datetime data = { 'date': [datetime(2023, 1, 1, 10), datetime(2023, 1, 1, 11), datetime(2023, 1, 2, 10)], 'value': [10, 20, 15], 'category': ['A', 'B', 'A'] } df = pd.DataFrame(data) ``` I want to group by the `date` field and get the sum of the `value` column and a list of `category`. I'm trying to do this: ```python result = df.groupby('date').agg({ 'value': 'sum', 'category': lambda x: list(x) }) ``` However, I'm running into the following behavior: ``` TypeError: unhashable type: 'list' ``` I've checked that my DataFrame is correctly formed before grouping, and I've tried using different aggregation functions, but I keep hitting this behavior. Is there a specific way I need to handle the aggregation for mixed data types? I'm using pandas version 1.3.5. Any insights or best practices on how to achieve this would be greatly appreciated! For context: I'm using Python on Windows. Any ideas what could be causing this?