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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:25:31  来源:igfitidea点击:

Pandas conditional on index column

pandas

提问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 cutoffis some datetime. How can I go about this?

cutoff日期时间在哪里。我该怎么办?

采纳答案by jezrael

It seems you need .indexfor compare DatetimeIndex:

看来你需要.index比较DatetimeIndex

data['DateTime'] = pandas.to_datetime (data['DateTime'])
data = data.set_index('DateTime')
data = data[data.index <= cutoff]

Also is sorted DatetimeIndexuse 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