通过字符串日期过滤熊猫的过滤数据
这样
的 | 框 |
---|---|
看起来 | 一个 |
我 | 有 |
数据 | |
| |
| |
| 一些价值 |
2011-01-04 | 一些价值 |
... | ... |
2012-01-02 | 某些价值 |
2012-01-03 | 某些值 |
2012-01-04 | 某些值 |
日期列(%y-%m-%d)是dtype dateTime64 [ns]和参数列为dtype float64
我也有一个字符串变量'limit_date'
我想获得一个DF,该df只有与limit_date之前的日期相对应的行。
我已经使用了这两种不同的方法,他们起作用了: df [df ['date']<'2011-01-01'] 20110101')
在两种情况下,我都会得到像这样看起来像这样的东西:
日期 | 参数 |
---|---|
2010-01-02 | 某些值 |
2010-01-03 | 某些值 |
2010-01-04 | 一些值 |
... | df.Query('date&lt ; |
2010-12-30 | 有些价值 |
2010-12-31 | 然而,一些价值 |
。如果我想使用字符串limit_date,我会遇到问题:
当我使用limit_date ='2011-01-01'和df [df ['date'']< limt_date]时,我得到一个空的df
- 当我使用limit_date ='2011-01-01'和df [df ['date']< limt_date]时,我会在使用limit_date ='1100101'和df.query('date&limit_date时,
')我有错误:
** undefinedVariable Error:名称'limit_date'未定义**
有人知道如何解决这个问题吗?
我需要在代码中使用字符串变量BC。此limit_date将在整个代码中更改。
I have a DataFrame that looks like this:
Date | Parameter |
---|---|
2010-01-02 | some value |
2010-01-03 | some value |
2010-01-04 | some value |
... | ... |
2011-01-02 | some value |
2011-01-03 | some value |
2011-01-04 | some value |
... | ... |
2012-01-02 | some value |
2012-01-03 | some value |
2012-01-04 | some value |
Date column (%Y-%m-%d) is Dtype datetime64[ns] and Parameter column is Dtype float64
I also have a string variable 'limit_date'
I want to get a DF that has just the rows corresponding to the dates before limit_date.
I have used this two different approaches and they worked:
df[df['Date']<'2011-01-01']
df.query('Date<20110101')
I get something that looks like this in both cases:
Date | Parameter |
---|---|
2010-01-02 | some value |
2010-01-03 | some value |
2010-01-04 | some value |
... | ... |
2010-12-30 | some value |
2010-12-31 | some value |
However. If I want to use the string limit_date I run into problems:
When I use limit_date = '2011-01-01' AND df[df['Date']<limit_date] I get an empty DF
When I use limit_date = '20110101' AND df.query('Date<limit_date') I get the error :
**UndefinedVariableError: name 'limit_date' is not defined**
Does anyone have any idea how to deal with this issue?
I need to use a string variable bc in my code this limit_date will change throughout the code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
dataframe.query
期望您参考列名;df.query('date&lt; limit_date')
正在尝试查询行,其中'date'date'
列中的值小于'中的值limit_date'
列。 (它如何知道'date'
是列,但是limit_date
是一个变量,鉴于您类似地引用它们?)以区分列和环境变量,您将需要在环境变量之前添加
@
。您的第一次尝试:
df [df ['date']&lt; limit_date]
在技术上是正确的,应该起作用。可能,您将以前的过滤器应用于数据框架并覆盖结果,因此现在,当您应用此过滤器时,它会导致空数据帧。刷新内核,从头开始。示例数据
DataFrame.query
expects you to reference column names;df.query('Date < limit_date')
is trying to query rows where the value in the'Date'
column is less than the value in the'limit_date'
column. (How would it know'Date'
is a column butlimit_date
is a variable, given that you refer to them similarly?)To distinguish between columns and an environment variable you will need to add
@
before the environment variables.Your first attempt:
df[df['Date'] < limit_date]
is technically correct and should work. Likely, you applied a previous filter to the DataFrame and overwrote the result, so that now when you apply this filter it results in an empty DataFrame. Refresh the kernel and start from the beginning.Sample Data