[英]Select the range of values in the column based on the interval of values in the other column in pandas
我有一个数据框:
scale year value
-10 2017 1 252
-9 2017 1 011
-8 2017 921
-7 2017 1 161
-6 2017 1 331
-5 2017 1 133
-4 2017 899
-3 2017 1 063
-2 2017 1 320
-1 2017 1 201
0 2017 1 212
1 2017 1 543
2 2017 656
3 2017 443
4 2017 1 398
5 2017 1 776
...
我需要在value
列中选择与scale
列中的值范围相对应的值,其中scale
介于0
和-7
之间。 在此示例中,值为1 212
到1 161
。
有关如何执行此操作的任何想法?
df = df[df['scale'].between(-7,0)]
print (df)
scale year value
3 -7 2017 1 161
4 -6 2017 1 331
5 -5 2017 1 133
6 -4 2017 899
7 -3 2017 1 063
8 -2 2017 1 320
9 -1 2017 1 201
10 0 2017 1 212
我们还可以使用.query
方法:
In [39]: df.query("-7 <= scale <= 0")
Out[39]:
scale year value
3 -7 2017 1 161
4 -6 2017 1 331
5 -5 2017 1 133
6 -4 2017 899
7 -3 2017 1 063
8 -2 2017 1 320
9 -1 2017 1 201
10 0 2017 1 212
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.