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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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
Method 2
Wen’s solution is really nice and intuitive. Here’s an alternative, calling repeat on df.values.
df code role persons 0 123 Janitor 3 1 123 Analyst 2 2 321 Vallet 2 3 321 Auditor 5 pd.DataFrame(df.values.repeat(df.persons, axis=0), columns=df.columns) code role persons 0 123 Janitor 3 1 123 Janitor 3 2 123 Janitor 3 3 123 Analyst 2 4 123 Analyst 2 5 321 Vallet 2 6 321 Vallet 2 7 321 Auditor 5 8 321 Auditor 5 9 321 Auditor 5 10 321 Auditor 5 11 321 Auditor 5
Method 3
Not enough reputation to comment, but building on @cs95’s answer and @lmiguelvargasf’s comment, one can preserve dtypes with:
pd.DataFrame(
df.values.repeat(df.persons, axis=0),
columns=df.columns,
).astype(df.dtypes)
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0