如果列是间隔数据类型,则数据框过滤列 [英] Dataframe filter a column if it is in an interval data type
问题描述
我有一个数据框数组,其中包含一些列,其中一个是时间"我想过滤特定时间间隔内的行
I have a dataframe array which included some column and one of them is 'time' i want to filter the rows which time is in a specific interval
为了简化问题,我制作了一个带有整数值和整数间隔的数据框
To simplify the problem i make a data frame with an integer value and an integer interval
data=pd.DataFrame({'A':[1,2,3,4,5,6,7,8,9]})
interval=pd.Interval(1,4)
data[data['A'] in interval]
显示:ValueError:系列的真值不明确.使用 a.empty、a.bool()、a.item()、a.any() 或 a.all().
It shows: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
但预期结果是 [2,3,4]
but the expected result is [2,3,4]
推荐答案
between
pandas.Series.
之间将返回一个布尔掩码,可用于过滤数据帧.
between
pandas.Series.between
will return a boolean mask that can be used to filter the dataframe.
data[data.A.between(1, 4)]
A
0 1
1 2
2 3
3 4
<小时>
查询
pandas.DataFrame.查询
也可用于过滤
data.query('1 <= A <= 4')
A
0 1
1 2
2 3
3 4
<小时>
isin
pandas.Series.isin
可以使用 range
在特殊的整数情况下使用.与 between
类似,我们创建了一个布尔掩码.
isin
pandas.Series.isin
can be used in the special integer case using range
. Similar to between
we create a boolean mask.
data[data.A.isin(range(1, 5))]
A
0 1
1 2
2 3
3 4
这篇关于如果列是间隔数据类型,则数据框过滤列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!