繁体   English   中英

Pandas - 选择以日期为键的多行和多列

[英]Pandas - selecting several rows and columns with date as key

我尝试 select 从这个 dataframe 几个特定的行和列:

    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

可以 select 一些行彼此跟随一个特定的列

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

我还尝试了 select 几行没有跟随 - 也不起作用

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')

我怎么能 select 几个特定的行和列 - 它们没有相互跟随?

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

将为您提供带有预选列的 dataframe。

您还可以使用:

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"]]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM