I have a data-frame like this:

and I want a new data-frame with a new column which contains a list like this:

How do I create a list inside a data-frame cell which contains the elements based on similar month?
Let try, set_index, groupby, and apply(list):
df.set_index('month', append=True).groupby(level=[0,1,2], sort=False)['from']\
.apply(list).reset_index('month')
Output:
month from
google 2016 2 [e]
apple 2016 1 [b, c]
2016 3 [l]
google 2016 3 [g]
Simpliest is use new functionality in pandas 0.20.0+ for groupby by levels and columns together:
df=df.groupby(['client','year','month'], sort=False)['from'].apply(list).reset_index('month')
print (df)
month from
client year
google 2016 2 [e]
apple 2016 1 [b, c]
2016 3 [l]
google 2016 3 [g]
For scalars with one element list solution is similar Wen - custom function with if else:
df=df.groupby(['client','year','month'], sort=False)['from'] \
.apply(lambda x: list(x) if len(x)>1 else x.iat[0]).reset_index('month')
print (df)
month from
client year
google 2016 2 e
apple 2016 1 [b, c]
2016 3 l
google 2016 3 g
And for strings joined by , use join instead list:
df=df.groupby(['client','year','month'], sort=False)['from']
.apply(', '.join).reset_index('month')
print (df)
month from
client year
google 2016 2 e
apple 2016 1 b, c
2016 3 l
google 2016 3 g
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With