'How to delete two rows before and two rows after minimum value of column python
I want to delete two rows before and two rows after minimum of column value , is there efficient way to do this in pandas
df=pd.DataFrame({'A':[1,1,1,1,1,0.5,2,2,2,2,2,2,2]})
df_out=pd.DataFrame({'A':[1,1,1,0.5,2,2,2,2,2]})
Solution 1:[1]
Based on @YOBEN_S's answer, but dropping only existing indexes:
df=pd.DataFrame({'A':[1,1,1,1,1,0.5,2,2,2,2,2,2,2]})
df_out=pd.DataFrame({'A':[1,1,1,0.5,2,2,2,2,2]})
min_idx=df.A.idxmin()
s1 = set(range(s-2,s+3))
s2 = set(df.index.tolist())
s1 = s1.intersection(s2)
df=df.drop(set(s1-{min_idx})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ralvi Isufaj |
