I have a data frame that I want to make some changes. Here's a sample:
d = {'username': ['a', 'a', 'b', 'a', 'a'],
'state': ['AR', 'AZ', 'CA', 'CO', 'NY'],
'status': ['ADD', 'ADD', 'REMOVE', 'ADD', 'REMOVE']}
df = pd.DataFrame(data=d)
I know how I can groupby and join the states:
df = df.fillna('').groupby(['username', 'status'], as_index=False)['state'] \
.apply(lambda x: ','.join(set(x))) \
.reset_index() \
.rename({0: 'state'}, axis=1)
But at the end I have something like this but still not what I need:
username status state
a ADD AR,AZ,CO
a REMOVE NY
b REMOVE CA
I want to produce this final report:
username ADD REMOVE
a AR,AZ,CO NY
b CA
Any ideas?
Thank you very much!