我正在尝试运行一个简单的脚本来按列对数据帧进行排序,但是会因此错误而被抛弃。
我正在尝试运行以下代码:
import pandas as pd
df = pd.read_excel('pandas_test.xlsx', sheetname='mydata')
print df
df.sort_values(by = 'Prediction', ascending=[False], inplace=True)
这是读出来的
Name Code Prediction Unnamed: 3 Unnamed: 4
0 Aiden ADN -0.904636 -0.897691 -0.897691
1 ARCHcoin ARCH 0.058027 0.279917 0.279917
2 ArtByte ABY 0.083942 0.396728 0.262711
3 BellaCoin BELA -0.622574 -0.357308 0.348456
4 Bitcoin Plus XBC -0.862629 -0.862629 -0.927854
5 BitcoinDark BTCD -0.349041 -0.495764 -0.071606
6 Bitmark BTM -0.712551 -0.686572 -0.686910
7 BitShares BTS 0.658777 0.645338 0.653746
8 BitShares PTS PTS -0.009761 -0.599135 -0.404015
9 Bitstar BITS 0.365824 0.388928 0.541017
10 BlackCoin BLK -0.153565 -0.040735 0.348264
11 Blocknet BLOCK 0.172974 0.403764 -0.011830
12 Boolberry BBR -0.556053 -0.570189 -0.441098
13 Boolberry BBR -0.556053 -0.570189 -0.441098
14 Burst BURST -0.302751 -0.301617 -0.201896
Traceback (most recent call last):
File "C:\Users\David\Desktop\Python\JSON\pandas_test.py", line 17, in <module>
df.sort_values(by = 'Prediction', ascending=[False], inplace=True)
File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 3098, in sort_values
k = self[by].values
File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 1969, in __getitem__
return self._getitem_column(key)
File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 1976, in _getitem_column
return self._get_item_cache(key)
File "C:\Python27\lib\site-packages\pandas\core\generic.py", line 1091, in _get_item_cache
values = self._data.get(item)
File "C:\Python27\lib\site-packages\pandas\core\internals.py", line 3211, in get
loc = self.items.get_loc(item)
File "C:\Python27\lib\site-packages\pandas\core\index.py", line 1759, in get_loc
return self._engine.get_loc(key)
File "pandas\index.pyx", line 137, in pandas.index.IndexEngine.get_loc (pandas\index.c:3979)
File "pandas\index.pyx", line 157, in pandas.index.IndexEngine.get_loc (pandas\index.c:3843)
File "pandas\hashtable.pyx", line 668, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12265)
File "pandas\hashtable.pyx", line 676, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12216)
KeyError: 'Prediction'
[Finished in 1.0s with exit code 1]
这不是让我发帖,因为它说我主要是代码,即使它主要是错误日志,我会在这里喋喋不休,直到我有足够的文字
答案 0 :(得分:0)
Sort_values已折旧!请改用df.sort。
df.sort(columns=None, axis=0, ascending=False,
inplace=False,kind='quicksort', na_position='last')
Inplace参数还需要一个boolian False 而不是列表 [False]