输入功能可搜索熊猫的开始和结束日期

时间:2020-09-28 15:08:38

标签: python pandas dataframe date input

我想让程序的用户输入开始和结束日期(所以熊猫只过滤必要的日期)。但不幸的是,它不能与输入功能一起使用。当我像这样开始输入日期时:“ 01-01-2000”,它可以正常工作。最后也一样:“ 2009年12月31日”。

start = year,month,day = input().split('-')
end = year,month,day = input().split('-')


df= pd.read_excel('path', skiprows = 6, converters = {'Date':str})
df= df[(df['Date'] >= start)]
df= df[(df['Date'] <= end)]
df

1 个答案:

答案 0 :(得分:0)

这是您要寻找的吗?

start = year,month,day = input().split('-')
start = datetime(year = int(year), month = int(month), day = int(day))

end = year, month, day = input().split('-')
end = datetime(year = int(year), month = int(month), day = int(day))

df = pd.read_excel('path', skiprows = 6)
df['Date'] = pd.to_datetime(df['Date'], format = "%Y-%m-%d", errors = "coerce", origin = "1900-01-01")

df = df[(df['Date'] >= start) & (df['Date'] <= end)]
df
相关问题