I am looking for a similar solution but I'm not able to find it and running out of time so I have to write this question please help me with it.
Firstly I am using pandas to get the output so please help me with the same.
So what I am trying to achieve is I want to assign a particular group of groupby to another row
so for instance I have a group in a groupby that I want in another column same row so I assign this value to another column and since it's writing row-wise so it'll write in the same row of different columns (I am doubtful about it too but will see after overcoming current roadblock) but instead of getting the value of the group I'm getting row number of group in other column example is below:
group_count=df['id'].value_counts()
group_count=group_count.to_dict()
for i in group_count:
for j in range(group_count[i]):
df['masked_id_' str(j 1)].iloc[j:j 1]=gk.get_group(i)['masked_id'].iloc[j:j 1]
All I am trying to achieve is to shift masked_id of the repeated id into a different column for instance I have 3 similar ids say:
id masked_id
1 23234 XXXX4
2 23234 XXXX4
3 23234 XXXX4
to turn this into something like below:
id masked_id_1 masked_id_2 masked_id_3
1 23234 XXXX4 XXXX4 XXXX4
but what I am getting is:
id masked_id_1 masked_id_2 masked_id_3
23234 1 2 3
Looking for any guidance to propagate in a similar direction and thanks a lot for lending your time.
Update(info about gk):
gk = df.groupby(by='id')
CodePudding user response:
Try this:
new_df = df.groupby('id')['masked_id'].agg(list).agg(pd.Series)
new_df.columns = 'masked_id_' (new_df.columns 1).astype(str)
new_df = new_df.reset_index()
Output:
>>> new_df
id masked_id_1 masked_id_2 masked_id_3
0 23234 XXXX4 XXXX4 XXXX4
