Pandas, Name Of The Column After A Group By Function
I have a simple Pandas Dataframe named purchase_cat_df: email cat 0 email1@gmail.com Mobiles & Tablets 1 email2@gmail.com Mobiles & Tablets
Solution 1:
If you want to keep your original index, you were probably looking for something like this:
purchase_cat_df.groupby('email', as_index=False)
as_index=False keeps the original index. You can then continue to address the column by its name.
Solution 2:
As @BrenBarn mentioned in the comments, the column with the lists doesn't have a name, because you've got a Series
, not a DataFrame
.
Try this:
test = purchase_cat_df.groupby('email').apply({'cat': list})
which returns a DataFrame
with email
set as the index and cat
as the name of the new column.
You can also use this when you have multiple columns you want to aggregate. See the documentation which has a few examples.
Post a Comment for "Pandas, Name Of The Column After A Group By Function"