consider a data frame defined like so:
import pandas as pd
test = pd.DataFrame({
'id' : ['a', 'b', 'c', 'd'],
'times' : [2, 3, 1, 5]
})
Is it possible to create a new data frame from this in which each row is repeated times times, such that the result looks like this:
>>> result id times 0 a 2 1 a 2 2 b 3 3 b 3 4 b 3 5 c 1 6 d 5 7 d 5 8 d 5 9 d 5 10 d 5
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
Use a combination of pd.DataFrame.loc and pd.Index.repeat
test.loc[test.index.repeat(test.times)] id times 0 a 2 0 a 2 1 b 3 1 b 3 1 b 3 2 c 1 3 d 5 3 d 5 3 d 5 3 d 5 3 d 5
To mimic your exact output, use reset_index
test.loc[test.index.repeat(test.times)].reset_index(drop=True) id times 0 a 2 1 a 2 2 b 3 3 b 3 4 b 3 5 c 1 6 d 5 7 d 5 8 d 5 9 d 5 10 d 5
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