Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何对通过数据库查询的数据进行数据切片?_Python_Python 2.7_Pandas_Influxdb Python - Fatal编程技术网

Python 如何对通过数据库查询的数据进行数据切片?

Python 如何对通过数据库查询的数据进行数据切片?,python,python-2.7,pandas,influxdb-python,Python,Python 2.7,Pandas,Influxdb Python,如何使用std输入功能对数据进行切片 我的数据如下: time duration 0 2018-07-04 12:19:29+00:00 4000000000 1 2018-07-04 12:20:30+00:00 4000000000 2 2018-07-04 12:21:31+00:00 3700000000 3 2018-07-04 12:22:31+00:00

如何使用std输入功能对数据进行切片

我的数据如下:

                 time                duration
0 2018-07-04 12:19:29+00:00         4000000000
1 2018-07-04 12:20:30+00:00         4000000000
2 2018-07-04 12:21:31+00:00         3700000000
3 2018-07-04 12:22:31+00:00         4100000000
4 2018-07-04 12:23:31+00:00         4100000000
我从XDB查询数据,然后转换成数据帧。 它给我一个错误SyntaxError:输入开始日期时标记无效:

我试图将时间列拆分为单独的日期和时间


还是没有运气

这应该是你要做的,从你的问题来看,我假设你想要获得两次之间的所有数据,使用date作为你指定的列来进行筛选。按照OP的要求,因为他使用的是Python 2.7,所以键入的输入应该像这样引用,以便用户的输入发挥作用

>>> input("please type the date: ") 
please type the date: 2016-08-09
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1
    2016-08-09
          ^
SyntaxError: invalid token
>>> input("please type the date: ") 
please type the date: "2018-07-09" 
'2018-07-09'
现在谈谈过滤数据的真正问题,下面是您可以做的:

newdf = df[(df["date"]>starting_date) and df["date"]<=ending_date] 

这会给你预期的结果。但是您必须确保df[date]具有与开始日期和结束日期相同的数据类型。

使用between。您是否尝试使用时间列过滤数据帧?因此,这就是您想要执行的操作df['time']=df['time'].dt.time df['date']=df['time'].dt.date?这在您的情况下不起作用吗?我已将时间列拆分为单独的日期和时间列,以便通过std输入方式使用日期列将数据帧分割为开始日期和结束日期。。。希望我清除了我需要的东西@user2906838你的std_输入正常吗?所以你的日期字段是datetime.date对吗?我希望从控制台@User2906838选择日期作为std输入是的,因此我假设你的输入代码很好,有什么问题吗?我已经更新了这个问题,请看一下拆分时间方法@User2906838我现在很不清楚,关于你到底想实现什么,还有什么不起作用。newdf=df[df[date]>开始日期和df[date]
newdf = df[(df["date"]>starting_date) and df["date"]<=ending_date]