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 inmatches:
diff = df.loc[i]['diff']
interval= round(diff/2) # index some place in the middle
df.loc[i-interval] = [0, 0, 0, diff-interval] # insertrow before matched index
df.loc[i]['diff'] =interval # may not need toupdate the interval
df.sort_index(inplace=False) # pandas appends bydefault 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"