i have a set of id having some category. However, i want each id have the same number of category which can be specified as df.id.category.unique().
For example:
Input
df1 = {"id": [1,1,1,2,2,3,3,3,3],
"category": ["a","b","e","a","d","a","b","c","d"]
}
output1 = pd.DataFrame(df1)
output1
Out[57]:
id category
0 1 a
1 1 b
2 1 e
3 2 a
4 2 d
5 3 a
6 3 b
7 3 c
8 3 d
The output should be:
Output
df2 = {"id": [1,1,1,1,1,2,2,2,2,2,3,3,3,3,3],
"category": sum([["a","b","c","d","e"] for _ in range(3)], [])}
output2 = pd.DataFrame(df2)
output2
Out[58]:
id category
0 1 a
1 1 b
2 1 c
3 1 d
4 1 e
5 2 a
6 2 b
7 2 c
8 2 d
9 2 e
10 3 a
11 3 b
12 3 c
13 3 d
14 3 e
If possible, I hope to have fast optimization. Thanks a lot!