Skip to content
Advertisement

How can I insert rows to Pandas dataframe depending on previous and next values?

I want to insert a row if the time values between the previous and next rows are high. Essentially I want to have a row for every 2 seconds. So in the below example I want to add 3 rows between 19 and 26. The time values will be 21, 23, 25 and I will later use interpolate method to fill X values for that rows.

Time X
15 150
16 172
16 175
18 193
19 190
26 232

Does anyone know how I can achieve this? Thank you

Advertisement

Answer

you can use append a list of dict:

df.append([{'Time':i} for i in range(21,26,2)], 
          ignore_index=True).sort_values('Time')
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement