Skip to content Skip to sidebar Skip to footer

Insert Row In Pandas Dataframe Based On A Condition

I'm using Pandas to process huge time series dataset. I would like add row between the rows in the dataframe if the difference between two consecutive indexes is greater than 5. Ac

Solution 1:

You have a head start. Add a diff column to allow for easier filtering.

Get indexes for data frames matching your rule and insert your row.

df['diff'] = df.index.to_series().diff().fillna(0).to_frame("diff")

matches = df[df['diff'] > 5].index.tolist()


for i in matches:
    diff = df.loc[i]['diff']
    interval = round(diff/2) # index some place in the middle
    df.loc[i-interval] = [0, 0, 0, diff-interval] # insert row before matched index
    df.loc[i]['diff'] = interval # may not need to update the interval

df.sort_index(inplace=False) # pandas appends by default so we should sort this

del df.diff # we can remove this 

Post a Comment for "Insert Row In Pandas Dataframe Based On A Condition"