这是我的代码:
import pandas as pd
data = pd.DataFrame({'Odd':[1,3,5,6,7,9], 'Even':[0,2,4,6,8,10]})
for i in reversed(data):
print(data['Odd'], data['Even'])
当我运行此代码时,出现以下错误:
Traceback (most recent call last):
File "C:\Python33\lib\site-packages\pandas\core\generic.py", line 665, in _get_item_cache
return cache[item]
KeyError: 5
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\*****\Documents\******\********\****.py", line 5, in <module>
for i in reversed(data):
File "C:\Python33\lib\site-packages\pandas\core\frame.py", line 2003, in __getitem__
return self._get_item_cache(key)
File "C:\Python33\lib\site-packages\pandas\core\generic.py", line 667, in _get_item_cache
values = self._data.get(item)
File "C:\Python33\lib\site-packages\pandas\core\internals.py", line 1656, in get
_, block = self._find_block(item)
File "C:\Python33\lib\site-packages\pandas\core\internals.py", line 1936, in _find_block
self._check_have(item)
File "C:\Python33\lib\site-packages\pandas\core\internals.py", line 1943, in _check_have
raise KeyError('no item named %s' % com.pprint_thing(item))
KeyError: 'no item named 5'
为什么会出现此错误?
我该如何解决?
正确的逆转方法是pandas.DataFrame
什么?
您是说
—
福雷
data[["Odd", "Even"]]
或更普遍地说data[data.columns[::-1]]
吗?
您仍然没有给出所需输出的示例。我知道如何解决这个事实
—
DSM
reversed(data)
是行不通的,但是我不知道为什么您要为框架中的每一列打印一次整个列Odd
和Even
列,如果您使用,代码将执行此操作reversed(list(data))
。
我想开始从我的数据框的端部线圈
—
迈克尔
"One"
不是中的列data
,而且我也不知道Two
是的变量还是错字"Two"
,这也不是列。您是否只是想反转列顺序?