gpt4 book ai didi

python - Pandas 时间子集时间序列 - 特定时间以上的日期

转载 作者:太空宇宙 更新时间:2023-11-04 03:06:23 25 4
gpt4 key购买 nike

如果 df[:'2012-01-07'] 返回日期低于 20120107 的子 DataFrame,什么返回日期高于 20120107? df['2012-01-07':] 没有...

最佳答案

对我来说它工作完美,但也许在实际数据中需要按 sort_index 排序索引:

df = pd.DataFrame({'a':[0,1,2,5,4]}, index=pd.date_range('2012-01-05', periods=5))
print (df)

#if need ascending sorting
df = df.sort_index()
a
2012-01-05 0
2012-01-06 1
2012-01-07 2
2012-01-08 5
2012-01-09 4

print (df[:'2012-01-07'])
a
2012-01-05 0
2012-01-06 1
2012-01-07 2

print (df['2012-01-07':])
a
2012-01-07 2
2012-01-08 5
2012-01-09 4

df = pd.DataFrame({'a':[0,1,2,5,4]}, index=pd.date_range('2012-01-05', periods=5))

#descending sorting
df = df.sort_index(ascending=False)

print (df)
a
2012-01-09 4
2012-01-08 5
2012-01-07 2
2012-01-06 1
2012-01-05 0

print (df[:'2012-01-07'])
a
2012-01-09 4
2012-01-08 5

print (df['2012-01-07':])
a
2012-01-07 2
2012-01-06 1
2012-01-05 0

关于python - Pandas 时间子集时间序列 - 特定时间以上的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39333262/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com