I have this dataframe:
JavaScript
x
7
1
c1 c2
2
0 B 1
3
1 A 2
4
2 B 5
5
3 A 3
6
4 A 7
7
My goal is to keep tracking the values in column2, based on the letters of column1 separated by(:), the output should look like this:
JavaScript
1
5
1
c1 list
2
0 A 2:3:7
3
1 B 1:5
4
5
What’s the most pythonic way to do this:
At the moment I’m able to group by the column 1 and I’m trying to use the apply() function, but I do not know how to map and make this list in the new column.
Advertisement
Answer
Try this:
JavaScript
1
2
1
df = df.groupby("c1")["c2"].apply(lambda x: ":".join([str(i) for i in x])).reset_index()
2