Python 如何使用标准输入函数从数据帧中分割日期?
我在中看到了文档,其中包含从数据帧中分割一系列数据的硬核脚本方法Python 如何使用标准输入函数从数据帧中分割日期?,python,pandas,stdin,Python,Pandas,Stdin,我在中看到了文档,其中包含从数据帧中分割一系列数据的硬核脚本方法 import pandas as pd import matplotlib.pyplot as plt df = pd.read_csv('d1.csv') df['time']=pd.to_datetime(df['time'], unit='ns') df = df.drop('name', 1) df['Time'] = df['time'].dt.time df['date'] = df['time'].dt.d
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('d1.csv')
df['time']=pd.to_datetime(df['time'], unit='ns')
df = df.drop('name', 1)
df['Time'] = df['time'].dt.time
df['date'] = df['time'].dt.date
df['date'] = pd.to_datetime(df['date'])
df = df.set_index(['date'])
df= df.loc['2018-07-04':'2018-07-05']
但我需要从标准输入函数中选择一系列数据,以及如何进行:
在控制台的表单中,不是使用df=df.loc['2018-07-04':'2018-07-05']
而是要求输入开始日期:
和输入停止日期:
,这样我将只获得所选日期范围的数据
实际上,我是这样做的:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('d1.csv')
df['time']=pd.to_datetime(df['time'], unit='ns')
df = df.drop('name', 1)
df['Time'] = df['time'].dt.time
df['date'] = df['time'].dt.date
df['date'] = pd.to_datetime(df['date'])
df = df.set_index(['date'])
Starting_Date = input(" Please Enter the Starting_Date : ")
Ending_Date = input(" Please Enter the Ending_Date : ")
data = df[Starting_Date:Ending_Date]
但这不起作用……请看一看。请试试这个。日期的格式为
年-月-日
,例如“2018-08-16”
from datetime import datetime
a = input('Starting_Date: ')
b = input('Ending_Date :')
starting_date = datetime.strptime(a, "%Y-%m-%d").date()
ending_date = datetime.strptime(b, "%Y-%m-%d").date()
df.loc[starting_date:ending_date]
希望对你有用:)请提供@Dawid_Sielski我已经更新了我的问题,请看一看:)@roganjosh我已经更新了我工作过的代码,现在问题已经完全完成,我感谢你的帮助。它给出了一个
类型错误:strTime()参数1必须是字符串,而不是int
这很奇怪。请在此链接中联机尝试我的代码:并说明它是否产生任何错误。它正在联机工作。但不是在我的系统上空闲。。。是因为我使用的是Python 2.7吗@Dawid_SielskiI正在使用python 3.6.2,这可能是问题所在,但为了确保我必须阅读python 2.7下有关此函数的文档。请共享文档链接。