我尝试从这个数据框架中选择几个特定的行和列:
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"]["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[["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]]