jeudi 26 août 2021

Replicating row in pandas Python with an If statement

I want to duplicate rows in the code below. If bugs equals 2 I want to duplicate the row two times, or if bugs equal to 3 I want to duplicate the row three times, and so on. In the example below, if I duplicate B (because it's equal than 2) I want its values to drop by one. The result is as follow in the_result_i_want:

import pandas as pd

df = pd.DataFrame(data={
  'id': ['A', 'B', 'C'],
  'bugs' : [  1,   2,   3],
})

the_result_i_want = pd.DataFrame(data={
  'id': ['A', 'B','B', 'C','C','C'],
  'bugs' : [1, 1,  1,   1,  1,   1],
})

Thank you in advance !

Aucun commentaire:

Enregistrer un commentaire