original DataFrame
dflabel value 0 a 1 1 a 2 2 b 3 3 a 4 4 b 5ds = df.groupby('label')['value'].apply(list)label a [1, 2, 4] b [3, 5]ds.explode()generatelabel a 1 a 2 a 4 b 3 b 5
What I need is a new column to represents the position of the item in the value column in the original list.
label value position
a 1 0
a 2 1
a 4 2
b 3 0
b 5 1
update:
Example above is not appropriate, suppose the original dataframe is
label value
0 a [1, 2]
1 a [2]
2 b [3]
3 a [4]
4 b [5]
df.explode('value') gives
label value
0 a 1
0 a 2
1 a 2
2 b 3
3 a 4
4 b 5
how I get
label value position
0 a 1 0
0 a 2 1
1 a 2 0
2 b 3 0
3 a 4 0
4 b 5 0
df['position'] = df.groupby('label').cumcount()df.sort_values("label")?