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

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

我正在做类似

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

然后我得到

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

我的 col2 中有 None 个值。有没有办法忽略那些 join?

尝试使用 lambda 表达式

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()