I have the following df:
code . role . persons
123 . Janitor . 3
123 . Analyst . 2
321 . Vallet . 2
321 . Auditor . 5
The first line means that I have 3 persons with the role Janitors. My problem is that I would need to have one line for each person. My df should look like this:
df:
code . role . persons
123 . Janitor . 3
123 . Janitor . 3
123 . Janitor . 3
123 . Analyst . 2
123 . Analyst . 2
321 . Vallet . 2
321 . Vallet . 2
321 . Auditor . 5
321 . Auditor . 5
321 . Auditor . 5
321 . Auditor . 5
321 . Auditor . 5
How could I do that using pandas?
reindex
+ repeat
df.reindex(df.index.repeat(df.persons))
Out[951]:
code . role ..1 persons
0 123 . Janitor . 3
0 123 . Janitor . 3
0 123 . Janitor . 3
1 123 . Analyst . 2
1 123 . Analyst . 2
2 321 . Vallet . 2
2 321 . Vallet . 2
3 321 . Auditor . 5
3 321 . Auditor . 5
3 321 . Auditor . 5
3 321 . Auditor . 5
3 321 . Auditor . 5
PS: you can add.reset_index(drop=True)
to get the new index