Python 使用复合索引获取数据帧的行号
我有一个目录,其中.csv文件包含60分钟的股票数据条,还有一个Python脚本,用于将它们全部加载到熊猫数据框中,并在符号和日期时间上建立索引,如下所示Python 使用复合索引获取数据帧的行号,python,pandas,dataframe,slice,Python,Pandas,Dataframe,Slice,我有一个目录,其中.csv文件包含60分钟的股票数据条,还有一个Python脚本,用于将它们全部加载到熊猫数据框中,并在符号和日期时间上建立索引,如下所示 import pandas as pd import glob import numpy as np allFiles = glob.glob("D:\\Data\\60 Min Bar Stocks\\*.csv") frame = pd.DataFrame() list_ = [] for file_ in allFiles:
import pandas as pd
import glob
import numpy as np
allFiles = glob.glob("D:\\Data\\60 Min Bar Stocks\\*.csv")
frame = pd.DataFrame()
list_ = []
for file_ in allFiles:
df = pd.read_csv(file_,index_col=None, header=0)
list_.append(df)
frame = pd.concat(list_)
frame.set_index(['Symbol','Date'],inplace=True)
print(frame.loc["AAL", :])
print(frame.loc["AAL", :].loc["05-Jun-2017 09:00", :])
第一次打印返回以下内容
Open High Low Close Volume
Date
05-Jun-2017 09:00 49.53 49.88 49.40 49.64 560155
05-Jun-2017 10:00 49.58 49.89 49.58 49.85 575165
Open 49.53
High 49.88
Low 49.40
Close 49.64
Volume 560155.00
Name: 05-Jun-2017 09:00, dtype: float64
第二次打印返回以下内容:
Open High Low Close Volume
Date
05-Jun-2017 09:00 49.53 49.88 49.40 49.64 560155
05-Jun-2017 10:00 49.58 49.89 49.58 49.85 575165
Open 49.53
High 49.88
Low 49.40
Close 49.64
Volume 560155.00
Name: 05-Jun-2017 09:00, dtype: float64
如何在数据帧中找到这一行的行索引,然后得到一个切片,该切片是由前一行、当前行和下一个10行组成的12行?我想您需要输入多索引的位置,然后选择:
但是,如果t
是第一个值或10
中的某些值是最后一个值,则会出现问题:
df1 = df.iloc[max(pos-1,0): min(pos+11,len(df.index))]
样本:
print (df)
Open High Low Close Volume
Symbol Date
AAL 05-Jun-2017 08:00 1.1801 1.1819 1.1801 1.1817 4
05-Jun-2017 09:00 1.1817 1.1818 1.1804 1.1814 18
05-Jun-2017 10:00 1.1817 1.1817 1.1802 1.1806 12
05-Jun-2017 11:00 1.1807 1.1815 1.1795 1.1808 26
05-Jun-2017 12:00 1.1803 1.1806 1.1790 1.1806 4
05-Jun-2017 13:00 1.1801 1.1801 1.1779 1.1786 23
05-Jun-2017 14:00 1.1795 1.1801 1.1776 1.1788 28
05-Jun-2017 15:00 1.1793 1.1795 1.1782 1.1789 10
05-Jun-2017 16:00 1.1780 1.1792 1.1776 1.1792 12
05-Jun-2017 17:00 1.1788 1.1792 1.1788 1.1791 4
不可能选择previousrow,因为如果索引为:
d = '05-Jun-2017 08:00'
s = 'AAL'
pos = df.index.get_loc((s,d))
df1 = df.iloc[max(pos-1,0): min(pos+10,len(df.index))]
print (df1)
Open High Low Close Volume
Symbol Date
AAL 05-Jun-2017 08:00 1.1801 1.1819 1.1801 1.1817 4
05-Jun-2017 09:00 1.1817 1.1818 1.1804 1.1814 18
05-Jun-2017 10:00 1.1817 1.1817 1.1802 1.1806 12
05-Jun-2017 11:00 1.1807 1.1815 1.1795 1.1808 26
05-Jun-2017 12:00 1.1803 1.1806 1.1790 1.1806 4
05-Jun-2017 13:00 1.1801 1.1801 1.1779 1.1786 23
05-Jun-2017 14:00 1.1795 1.1801 1.1776 1.1788 28
05-Jun-2017 15:00 1.1793 1.1795 1.1782 1.1789 10
05-Jun-2017 16:00 1.1780 1.1792 1.1776 1.1792 12
05-Jun-2017 17:00 1.1788 1.1792 1.1788 1.1791 4
不可能选择下一行的所有10个,因为t
是3.rd
后面的值:
d = '05-Jun-2017 15:00'
s = 'AAL'
pos = df.index.get_loc((s,d))
df1 = df.iloc[max(pos-1,0): min(pos+10,len(df.index))]
print (df1)
Open High Low Close Volume
Symbol Date
AAL 05-Jun-2017 14:00 1.1795 1.1801 1.1776 1.1788 28
05-Jun-2017 15:00 1.1793 1.1795 1.1782 1.1789 10
05-Jun-2017 16:00 1.1780 1.1792 1.1776 1.1792 12
05-Jun-2017 17:00 1.1788 1.1792 1.1788 1.1791 4
谢谢这是可行的,但有一个小问题,我相信这与数据帧的总体排序顺序有关。文件中的最后一个符号是锡安,“AAL”后的符号是“AAPL”,因此当我使用您的解决方案时,我实际得到的是2017年6月6日锡安的最后一个条目,2017年6月7日AAL的8个条目,然后2017年6月7日AAPL的前3个条目。。。我怎样才能按符号然后按日期排序,这样这个切片只返回AAL的行?明白了!我添加了以下行,现在它可以工作了;frame.sort\u索引(inplace=True)