Search code examples
pythonpandasjoinaggregatenonetype

Aggregating rows based on one column with ", ".join -- TypeError: sequence item 0: expected str instance, NoneType found


I'm doing something like

dfAgg =   df.groupby('col1').agg({'col2': (", ").join, 
                                  'col3':'first',
                                  'col4': sum,
                                }).reset_index()

and I get

TypeError: sequence item 0: expected str instance, NoneType found

There are None values in my col2. Is there a way to ignore those with join?


Solution

  • Give it a try with a lambda expression

    custom_agg = lambda ar: ', '.join([item for item in ar if item])
    
    dfAgg = df.groupby('col1').agg({'col2': custom_agg, 
                                      'col3':'first',
                                      'col4': sum,
                                    }).reset_index()