ben890
ben890

Reputation: 1133

Expand DataFrame based on list and add index

I have a dataframe that looks like the following:

enter image description here

My goal is to expand the dataframe for each item in the list and add a index column that refers to position in the source list.

enter image description here

WHat is the best way to do this?

Upvotes: 2

Views: 162

Answers (1)

Code Different
Code Different

Reputation: 93161

Use explode:

df = df.explode('Values')
df['index'] = df.groupby('Name').cumcount()

Upvotes: 2

Related Questions