如何通过对列值范围进行比较来提取行

时间:2019-10-31 02:19:29

标签: python-3.x pandas numpy pandas-groupby

我有带有列值,ID,距离和distance2的数据框。当列距离或distance2的值从0更改为距离列4000到5000的值范围时,以及距离2列的值从0更改为范围3000到4000时,我想提取上一行。

这是我的示例df

df=pd.DataFrame({'value':[3,4,7,8,11,20,15,20,15,16],
             'ID':[2,2,8,8,8,2,2,2,5,5],
             'distance':[0,0,0,4008,0,0,4820,0,0,0],'distance2':[0,0,0,3006,0,0,0,1,3990,0]})





    value  ID  distance  distance2
0      3   2         0          0
1      4   2         0          0
2      7   8         0          0
3      8   8      4008       3006
4     11   8         0          0
5     20   2         0          0
6     15   2      4820          0
7     20   2         0          1
8     15   5         0       3990
9     16   5         0          0
desired output

  value  ID  distance  distance2
0      7   8      4008       3006
1     20   2      4820          0
2     20   2         0       3990

1 个答案:

答案 0 :(得分:0)

我试图修改iterrows pandas get next rows value接受的答案,这似乎可行:

row_iterator = df.iterrows()
_, last = next(row_iterator)
df_new = []

for index, row in row_iterator:
    if ((4000 < row.distance < 5000) & (last.distance == 0)) | ((3000 < row.distance2 < 4000) & (last.distance2 == 0)):
        df_new.append([last.value, last.ID, row.distance, row.distance2])
    last = row
df_new = pd.DataFrame(df_new, columns=df.columns)