我有这个数据集
df = pd.DataFrame({'Values': [0, 1, 2, 3, 4]},
index = [pd.Timestamp('20130101 09:00:00'),
pd.Timestamp('20130101 09:00:02'),
pd.Timestamp('20130101 09:00:03'),
pd.Timestamp('20130101 09:00:05'),
pd.Timestamp('20130101 09:00:06')])
我必须在我的数据库中找到这种模式
pattern = [1,2,3]
所以,它必须回到我身边(例如):
20130101 09:00:02 1
20130101 09:00:03 2
20130101 09:00:05 3
我正在尝试使用熊猫,有人可以帮助我吗?
答案 0 :(得分:2)
希望这有帮助!
import pandas as pd
df = pd.DataFrame({'Values': [0, 1, 2, 3, 4]},
index = [pd.Timestamp('20130101 09:00:00'),
pd.Timestamp('20130101 09:00:02'),
pd.Timestamp('20130101 09:00:03'),
pd.Timestamp('20130101 09:00:05'),
pd.Timestamp('20130101 09:00:06')])
pattern = [1,2,3]
print df.iloc[[int(df.index.get_indexer_for((df[df.Values==i].index))) for i in pattern]]