2017-05-15 27 views
1

我只想獲取索引列。從數據幀中獲取索引列錯誤

import pandas as pd 
df1=pd.read_csv(path1, index_col='ID') 
df1.head() 


     VAR1 VAR2 VAR3 OUTCOME 
ID 
28677 28  1 0.0  0 
27170 59  1 0.0  1 
39245 65  1 0.0  1 
31880 19  1 0.0  0 
41441 24  1 0.0  1 

我能得到多少列,如:

df1["VAR1"] 

ID 
28677  28 
27170  59 
39245  65 
31880  19 
41441  24 
31070  77 
39334  63 
.... 
38348  23 
38278  52 
28177  58 

,但是,我不能讓索引列:

>>> df1["ID"] 
Traceback (most recent call last): 
File "C:\Anaconda3\lib\site-packages\pandas\indexes\base.py", line 2134, in get_loc 
return self._engine.get_loc(key) 
File "pandas\index.pyx", line 132, in pandas.index.IndexEngine.get_loc (pandas\index.c:4433) 
File "pandas\index.pyx", line 154, in pandas.index.IndexEngine.get_loc (pandas\index.c:4279) 
File "pandas\src\hashtable_class_helper.pxi", line 732, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:13742) 
File "pandas\src\hashtable_class_helper.pxi", line 740, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:13696) 
KeyError: 'ID' 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
File "<stdin>", line 1, in <module> 
File "C:\Anaconda3\lib\site-packages\pandas\core\frame.py", line 2059, in __getitem__ 
return self._getitem_column(key) 
File "C:\Anaconda3\lib\site-packages\pandas\core\frame.py", line 2066, in _getitem_column 
return self._get_item_cache(key) 
File "C:\Anaconda3\lib\site-packages\pandas\core\generic.py", line 1386, in _get_item_cache 
values = self._data.get(item) 
File "C:\Anaconda3\lib\site-packages\pandas\core\internals.py", line 3543, in get 
loc = self.items.get_loc(item) 
File "C:\Anaconda3\lib\site-packages\pandas\indexes\base.py", line 2136, in get_loc 
return self._engine.get_loc(self._maybe_cast_indexer(key)) 
File "pandas\index.pyx", line 132, in pandas.index.IndexEngine.get_loc (pandas\index.c:4433) 
File "pandas\index.pyx", line 154, in pandas.index.IndexEngine.get_loc (pandas\index.c:4279) 
File "pandas\src\hashtable_class_helper.pxi", line 732, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:13742) 
File "pandas\src\hashtable_class_helper.pxi", line 740, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:13696) 
KeyError: 'ID' 

我想索引列的列表。 怎麼辦? 爲什麼我得到錯誤?

如果我想合併兩個數據幀使用索引列,該怎麼做?

回答

1

第一列是index因此對於選擇使用:

print (df1.index) 
Int64Index([28677, 27170, 39245, 31880, 41441], dtype='int64', name='ID') 

但是,如果可能的話MultiIndexindex使用get_level_values

print (df1.index.get_level_values('ID')) 
Int64Index([28677, 27170, 39245, 31880, 41441], dtype='int64', name='ID') 
+0

如果我想合併兩個數據框使用該索引列,如何做到這一點? – user504909

+1

您可以在['merge']中使用'left_index = True'和'right_index = True'參數(http://pandas.pydata.org/pandas-docs/stable/generated/pandas.merge.html)。 – jezrael

+0

更好的是解釋[這裏](http://stackoverflow.com/a/41265154/2901002) – jezrael

0

可以使用df.index屬性:

df.index (or df.index.values for numpy array) 

pd.Series(df.index) 
+0

如果我想合併兩個數據幀使用那個索引列,該怎麼做呢? – user504909

+0

left_on和right_on在我猜的合併參數。 –