如何使用另一个日期时间索引获取带有日期时间索引的Pandas数据框中的行? [英] How to get the rows in Pandas dataframe with datetime index using another datetime index?
本文介绍了如何使用另一个日期时间索引获取带有日期时间索引的Pandas数据框中的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有以下日期时间索引的Pandas数据框:
I have a Pandas dataframe with the following datetime index:
DatetimeIndex(['2020-01-02', '2020-01-03', '2020-01-06', '2020-01-07',
'2020-01-08', '2020-01-09', '2020-01-10', '2020-01-13',
'2020-01-14', '2020-01-15',
...
'2020-01-17', '2020-01-21', '2020-01-22', '2020-01-23',
'2020-01-24', '2020-01-27', '2020-01-28', '2020-01-29',
'2020-01-30', '2020-01-31'],
dtype='datetime64[ns]', name='Date', length=49098, freq=None)
我想获取与以下日期时间索引相交的行:
I want to get the rows which intersect with the following datetime index:
DatetimeIndex(['2020-01-02', '2020-01-03', '2020-01-06', '2020-01-07',
'2020-01-08', '2020-01-09', '2020-01-10'],
dtype='datetime64[ns]', name='Date', freq=None)
最自然的方法(又称"Pythonic")是什么?
What is the most natural (aka "Pythonic") way to do it?
推荐答案
Or, if the indexes have not been previously defined:
idx = df1.index.intersection(df2.index)
这篇关于如何使用另一个日期时间索引获取带有日期时间索引的Pandas数据框中的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文