Python Pandas-选择几个以日期为键的行和列

Python Pandas-选择几个以日期为键的行和列,python,pandas,Python,Pandas,我尝试从此数据框中选择几个特定的行和列: Open High Low Close Volume Dividends Stock Splits Date 2020-07-17 387.95 388.59 383.36 385.31 23046700 0 0 2020-07-20 385.67 394.00 384.25 393.43 22579500 0 0 2020-07

我尝试从此数据框中选择几个特定的行和列:

    Open    High    Low Close   Volume  Dividends   Stock Splits
Date                            
2020-07-17  387.95  388.59  383.36  385.31  23046700    0   0
2020-07-20  385.67  394.00  384.25  393.43  22579500    0   0
2020-07-21  396.69  397.00  386.97  388.00  25911500    0   0
2020-07-22  386.77  391.90  386.41  389.09  22215400    0   0
2020-07-23  387.99  388.31  384.25  385.17  4554225 0   0
可以选择一些行,这些行后面有一个特定的列

hist["2020-07-20":"2020-07-22"]["Close"]

Date
2020-07-20    393.43
2020-07-21    388.00
2020-07-22    389.09
Name: Close, dtype: float64
当我尝试下面的更多列时,我会出现以下错误:

hist["2020-07-20":"2020-07-22", "Open":"Close"]

TypeError                                 Traceback (most recent call last)
<ipython-input-25-57b43e76004f> in <module>
----> 1 hist["2020-07-20":"2020-07-22", "Open":"Close"]

c:\users\polzi\appdata\local\programs\python\python37\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   2798             if self.columns.nlevels > 1:
   2799                 return self._getitem_multilevel(key)
-> 2800             indexer = self.columns.get_loc(key)
   2801             if is_integer(indexer):
   2802                 indexer = [indexer]

c:\users\polzi\appdata\local\programs\python\python37\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2644                 )
   2645             try:
-> 2646                 return self._engine.get_loc(key)
   2647             except KeyError:
   2648                 return self._engine.get_loc(self._maybe_cast_indexer(key))

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

TypeError: '(slice('2020-07-20', '2020-07-22', None), slice('Open', 'Close', None))' is an invalid key
hist[“2020-07-20”:“2020-07-22”,“打开”:“关闭”]
TypeError回溯(最近一次调用上次)
在里面
---->1历史[“2020-07-20”:“2020-07-22”,“开放”:“关闭”]
c:\users\polzi\appdata\local\programs\python37\lib\site packages\pandas\core\frame.py in\uuuuu\getitem\uuuu(self,key)
2798如果self.columns.nlevels>1:
2799返回自我。\u获取项目\u多级(键)
->2800索引器=self.columns.get_loc(键)
2801如果是_整数(索引器):
2802索引器=[索引器]
c:\users\polzi\appdata\local\programs\python37\lib\site packages\pandas\core\index\base.py in get\u loc(self、key、method、tolerance)
2644                 )
2645请尝试:
->2646返回自引擎。获取位置(钥匙)
2647键错误除外:
2648返回self.\u引擎。获取self.\u loc(self.\u可能\u cast\u索引器(键))
熊猫\\u libs\index.pyx在熊猫中。\ u libs.index.IndexEngine.get_loc()
熊猫\\u libs\index.pyx在熊猫中。\ u libs.index.IndexEngine.get_loc()
TypeError:'(slice('2020-07-20','2020-07-22',无),slice('Open','Close',无))'是无效键
我还试着选择了几行,它们不在后面,也不起作用

hist["2020-07-20","2020-07-22"]["Low"]

KeyError                                  Traceback (most recent call last)
c:\users\polzi\appdata\local\programs\python\python37\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2645             try:
-> 2646                 return self._engine.get_loc(key)
   2647             except KeyError:

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: ('2020-07-20', '2020-07-22')

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-26-aefccd2025a5> in <module>
----> 1 hist["2020-07-20","2020-07-22"]["Low"]

c:\users\polzi\appdata\local\programs\python\python37\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   2798             if self.columns.nlevels > 1:
   2799                 return self._getitem_multilevel(key)
-> 2800             indexer = self.columns.get_loc(key)
   2801             if is_integer(indexer):
   2802                 indexer = [indexer]

c:\users\polzi\appdata\local\programs\python\python37\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2646                 return self._engine.get_loc(key)
   2647             except KeyError:
-> 2648                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2649         indexer = self.get_indexer([key], method=method, tolerance=tolerance)
   2650         if indexer.ndim > 1 or indexer.size > 1:

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: ('2020-07-20', '2020-07-22')
hist[“2020-07-20”、“2020-07-22”][“低”]
KeyError回溯(最近一次呼叫最后一次)
c:\users\polzi\appdata\local\programs\python37\lib\site packages\pandas\core\index\base.py in get\u loc(self、key、method、tolerance)
2645请尝试:
->2646返回自引擎。获取位置(钥匙)
2647键错误除外:
熊猫\\u libs\index.pyx在熊猫中。\ u libs.index.IndexEngine.get_loc()
熊猫\\u libs\index.pyx在熊猫中。\ u libs.index.IndexEngine.get_loc()
pandas\\u libs\hashtable\u class\u helper.pxi在pandas.\u libs.hashtable.PyObjectHashTable.get\u item()中
pandas\\u libs\hashtable\u class\u helper.pxi在pandas.\u libs.hashtable.PyObjectHashTable.get\u item()中
键错误:('2020-07-20'、'2020-07-22')
在处理上述异常期间,发生了另一个异常:
KeyError回溯(最近一次呼叫最后一次)
在里面
---->1历史[“2020-07-20”,“2020-07-22”][“低”]
c:\users\polzi\appdata\local\programs\python37\lib\site packages\pandas\core\frame.py in\uuuuu\getitem\uuuu(self,key)
2798如果self.columns.nlevels>1:
2799返回自我。\u获取项目\u多级(键)
->2800索引器=self.columns.get_loc(键)
2801如果是_整数(索引器):
2802索引器=[索引器]
c:\users\polzi\appdata\local\programs\python37\lib\site packages\pandas\core\index\base.py in get\u loc(self、key、method、tolerance)
2646返回自引擎。获取位置(钥匙)
2647键错误除外:
->2648返回self.\u引擎。获取self.\u loc(self.\u可能\u cast\u索引器(键))
2649 indexer=self.get\u indexer([key],method=method,tolerance=tolerance)
2650如果indexer.ndim>1或indexer.size>1:
熊猫\\u libs\index.pyx在熊猫中。\ u libs.index.IndexEngine.get_loc()
熊猫\\u libs\index.pyx在熊猫中。\ u libs.index.IndexEngine.get_loc()
pandas\\u libs\hashtable\u class\u helper.pxi在pandas.\u libs.hashtable.PyObjectHashTable.get\u item()中
pandas\\u libs\hashtable\u class\u helper.pxi在pandas.\u libs.hashtable.PyObjectHashTable.get\u item()中
键错误:('2020-07-20'、'2020-07-22')
我如何选择几个不在后面的特定行和列

hist[["Open","High","Low","Close"]]["2020-07-20":"2020-07-22"]
将为您提供一个带有预选列的数据帧

您还可以使用:

hist[hist.columns[0:4]]["2020-07-20":"2020-07-22"]
如果行不在下面,则可以使用:

hist[hist.index.isin(["2020-07-20","2020-07-22"])][hist.columns[0:4]]
如果行和列都是任意的,则可以使用

hist[hist.index.isin(["2020-07-20","2020-07-22"])][["Open","Close"]]
将为您提供一个带有预选列的数据帧

您还可以使用:

hist[hist.columns[0:4]]["2020-07-20":"2020-07-22"]
如果行不在下面,则可以使用:

hist[hist.index.isin(["2020-07-20","2020-07-22"])][hist.columns[0:4]]
如果行和列都是任意的,则可以使用

hist[hist.index.isin(["2020-07-20","2020-07-22"])][["Open","Close"]]

hist.loc[“2020-07-20”:“2020-07-22”,“Open”:“Close”]
hist.loc[“2020-07-20”:“2020-07-22”,“Open”:“Close”]
好的,这很有帮助,但我在第三条语句中出现了一个错误-结尾缺少了一些东西-但不是]除此之外,当我不需要以下行和列时,我该怎么做?我该怎么做?顺便说一句:Pandas似乎是一个非常庞大和流行的模块-但我从未见过这样一种非初始逻辑-有时行先列后列-有时列先列后行-有时列表形式-有时切片形式-目前我完全搞不懂…:=)在实践中,我将主要使用Hist[条件] [列的列表]来获取只读值和Hist.LOC[条件,列修改]来编辑数据文件,我承认前两个公式看起来不自然,因为它们相对于我认为的“自然”而颠倒了列和行。orderOk这很有帮助,但我在第3条语句中遇到了一个错误-结尾缺少了一些东西-但它不是]除此之外,当我不需要下面的行和列时,我该怎么做-我该怎么做?顺便说一句:Pandas似乎是一个非常庞大和流行的模块-但我从未见过这样一种非初始逻辑-有时行先列后列-有时列先列后行-有时列表形式-有时切片形式-目前我完全搞不懂…:=)在实践中,我主要使用Hist[条件] [列的列表]来获取只读值和Hist.LOC[条件,列修改]来编辑数据文件,我承认前两个公式看起来不自然,因为它们相对于我认为的“自然”顺序颠倒了列和行。