Pandas 以索引列为条件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43499216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas conditional on index column
提问by John
I have a pandas DataFrame, and set index to be the DateTime column:
我有一个 Pandas DataFrame,并将索引设置为 DateTime 列:
data['DateTime'] = pandas.to_datetime (data['DateTime'])
data = data.set_index('DateTime')
which I need to interpolate the data. However, this indexing later prevents me from doing
我需要插入数据。然而,这个索引后来阻止我做
data = data[pandas.to_datetime (data['DateTime']) <= cutoff]
where cutoff
is some datetime. How can I go about this?
cutoff
日期时间在哪里。我该怎么办?
采纳答案by jezrael
It seems you need .index
for compare DatetimeIndex
:
看来你需要.index
比较DatetimeIndex
:
data['DateTime'] = pandas.to_datetime (data['DateTime'])
data = data.set_index('DateTime')
data = data[data.index <= cutoff]
Also is sorted DatetimeIndex
use loc
:
也是排序DatetimeIndex
使用loc
:
data1 = data1.loc[:cutoff]
Sample:
样本:
rng = pd.date_range('2017-04-03', periods=10)
data = pd.DataFrame({'a': range(10)}, index=rng)
print (data)
a
2017-04-03 0
2017-04-04 1
2017-04-05 2
2017-04-06 3
2017-04-07 4
2017-04-08 5
2017-04-09 6
2017-04-10 7
2017-04-11 8
2017-04-12 9
cutoff = '2017-04-08'
data1 = data[data.index <= cutoff]
print (data1)
a
2017-04-03 0
2017-04-04 1
2017-04-05 2
2017-04-06 3
2017-04-07 4
2017-04-08 5
data1 = data1.loc[:cutoff]
print (data1)
a
2017-04-03 0
2017-04-04 1
2017-04-05 2
2017-04-06 3
2017-04-07 4
2017-04-08 5
Thanks piRSquared:
感谢piRSquared:
data1 = data1[:cutoff]
print (data1)
a
2017-04-03 0
2017-04-04 1
2017-04-05 2
2017-04-06 3
2017-04-07 4
2017-04-08 5