简体   繁体   English

根据条件选择 pandas Dataframe 中的行不起作用

[英]Selecting Rows in pandas Dataframe based on condition doesnt work

I have a dataframe with a "Timestamp" column like this:我有一个带有“时间戳”列的 dataframe,如下所示:

df[Timestamp]

0            1.341709
1            1.343688
2            1.344503
3            1.344593
4            1.344700
              ...    
1263453    413.056745
1263454    413.056836
1263455    413.056945
1263456    413.057046
1263457    413.057153
Name: Timestamp, Length: 1263458, dtype: float64

Now i have two variables to define a start and end of an interval like so:现在我有两个变量来定义间隔的开始和结束,如下所示:

start = 10
end = 15

To select all rows in the Dataframe where the Timestamp lies between "start" and "end" I use a query approach:对于 select 中时间戳位于“开始”和“结束”之间的 Dataframe 中的所有行,我使用查询方法:

df_want = df.query("@start <= Timestamp < @end")

This gives me a Typeerror though不过,这给了我一个 Typeerror

TypeError: '<=' not supported between instances of 'int' and 'type'

Why does this not work, shouldnt Timestamp be of type 'float64'?为什么这不起作用,时间戳不应该是“float64”类型? Why is it just 'type'?为什么它只是“类型”?

You need to do the following:您需要执行以下操作:

df_want = df[df['Timestamp'].between(start,end)]

With the variables随着变量

df[(df['Timestamp'] >= start) & (df['Timestamp'] <= end)]

With the values harcoded:使用硬编码的值:

df[(df['Timestamp'] >= 15) & (df['Timestamp'] <= 30)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM