2017-04-12 37 views
2

索引數據幀熊貓我有這種格式的文件文本:與pandas.read_csv

  Value_1  Value_2  Value_3  Value_4  Value_5 
10 MSX 0.40748157 0.223845099 0.14417411 0.434948943 0.804686917 
20 MSX 0.679787708 0.669394102 0.09966376 0.295964602 0.741914305 
30 MSX 0.746162656 0.325330912 0.739320885 0.999178387 0.492413352 
40 MSX 0.320922967 0.015966342 0.599784939 0.621426555 0.393276648 
50 MSX 0.306445206 0.412270302 0.18820778 0.605985775 0.21907484 
60 MSX 0.238848223 0.188215196 0.546380095 0.683780446 0.868291677 
70 MSX 0.181441931 0.868270404 0.483950835 0.398810737 0.874501167 
80 MSX 0.559737008 0.991398039 0.737921625 0.998737594 0.033864231 
90 MSX 0.648728689 0.832948225 0.558207809 0.712151967 0.11245818 
100 MSX 0.467005098 0.378185352 0.347756793 0.098410037 0.189492058 

我閱讀pandas.read_csv這個文件是這樣的:

import pandas 

db = "data.txt" 

a = pandas.read_csv(db, sep='\t') 

伊爾想得值例如第40行和第Value_3列。

當我使用:

a["Value_3"] 

我有VALUE_3所有列值:

>>> a["Value_3"] 
    10 MSX 0.144174 
    20 MSX 0.099664 
    30 MSX 0.739321 
    40 MSX 0.599785 
    50 MSX 0.188208 
    60 MSX 0.546380 
    70 MSX 0.483951 
    80 MSX 0.737922 
    90 MSX 0.558208 
    100 MSX 0.347757 
    Name: Value_3 

當我使用一個[ 「40」, 「VALUE_3」]我有這樣的錯誤消息:

Traceback (most recent call last): 
    File "<pyshell#20>", line 1, in <module> 
    a["40","Value_3"] 
    File "C:\Program Files (x86)\python27\lib\site-packages\pandas\core\frame.py", line 1704, in __getitem__ 
    return self._get_item_cache(key) 
    File "C:\Program Files (x86)\python27\lib\site-packages\pandas\core\generic.py", line 474, in _get_item_cache 
    values = self._data.get(item) 
    File "C:\Program Files (x86)\python27\lib\site-packages\pandas\core\internals.py", line 827, in get 
    _, block = self._find_block(item) 
    File "C:\Program Files (x86)\python27\lib\site-packages\pandas\core\internals.py", line 941, in _find_block 
    self._check_have(item) 
    File "C:\Program Files (x86)\python27\lib\site-packages\pandas\core\internals.py", line 948, in _check_have 
    raise KeyError('no item named %s' % str(item)) 
KeyError: "no item named ('40', 'Value_3')" 

誰能幫幫我嗎?

回答

1

看來你得到MultiIndex,用於選擇是可能的使用slicers

print (df.index) 
MultiIndex(levels=[[10, 20, 30, 40, 50, 60, 70, 80, 90, 100], ['MSX']], 
      labels=[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]) 


idx = pd.IndexSlice 
print (df.loc[idx[40,'MSX'],'Value_3']) 
0.599784939 

print (df.loc[(40,'MSX'),'Value_3']) 
0.599784939 

rename_axisreset_index爲集索引名和可轉換索引列:

df = df.rename_axis(['a','b']).reset_index(level=1) 
print (df) 
     b Value_1 Value_2 Value_3 Value_4 Value_5 
a               
10 MSX 0.407482 0.223845 0.144174 0.434949 0.804687 
20 MSX 0.679788 0.669394 0.099664 0.295965 0.741914 
30 MSX 0.746163 0.325331 0.739321 0.999178 0.492413 
40 MSX 0.320923 0.015966 0.599785 0.621427 0.393277 
50 MSX 0.306445 0.412270 0.188208 0.605986 0.219075 
60 MSX 0.238848 0.188215 0.546380 0.683780 0.868292 
70 MSX 0.181442 0.868270 0.483951 0.398811 0.874501 
80 MSX 0.559737 0.991398 0.737922 0.998738 0.033864 
90 MSX 0.648729 0.832948 0.558208 0.712152 0.112458 
100 MSX 0.467005 0.378185 0.347757 0.098410 0.189492 

print (df.loc[40, 'Value_3']) 
0.599784939 

或者:

df = df.rename_axis(['a','b']).reset_index() 
print (df) 
    a b Value_1 Value_2 Value_3 Value_4 Value_5 
0 10 MSX 0.407482 0.223845 0.144174 0.434949 0.804687 
1 20 MSX 0.679788 0.669394 0.099664 0.295965 0.741914 
2 30 MSX 0.746163 0.325331 0.739321 0.999178 0.492413 
3 40 MSX 0.320923 0.015966 0.599785 0.621427 0.393277 
4 50 MSX 0.306445 0.412270 0.188208 0.605986 0.219075 
5 60 MSX 0.238848 0.188215 0.546380 0.683780 0.868292 
6 70 MSX 0.181442 0.868270 0.483951 0.398811 0.874501 
7 80 MSX 0.559737 0.991398 0.737922 0.998738 0.033864 
8 90 MSX 0.648729 0.832948 0.558208 0.712152 0.112458 
9 100 MSX 0.467005 0.378185 0.347757 0.098410 0.189492 

print (df.loc[3, 'Value_3']) 
0.599784939 

編輯:

或者,如果indexMultiIndex

print (df.index) 
Index(['10 MSX', '20 MSX', '30 MSX', '40 MSX', '50 MSX', '60 MSX', '70 MSX', 
     '80 MSX', '90 MSX', '100 MSX'], 
     dtype='object') 

print (df.loc['40 MSX', 'Value_3']) 
0.599784939 
+0

非常感謝您的回答,但我用熊貓版本0.09.0和LOC不落實,因爲我不是我的電腦的管理員,我不能升級版本的熊貓。 – nekcorp

+0

我不確定,但[文檔](http://pandas.pydata.org/pandas-docs/version/0.09.0/)不是你的版本,最後是'0.12.0'。所以我不知道如何在您的版本中選擇。而最後一個版本是'0.19.2',所以建議升級,但我明白有時候這並不容易。但是如果'df = df.reset_index(level = 1)'它應該有效。 – jezrael

+0

@nekcorp,絕對是升級的時間! [0.9.0](http://pandas-docs.github.io/pandas-docs-travis/release.html#id42)已近5歲!每個新的Pandas版本都增加了很多不錯的功能並修復了很多錯誤。 – MaxU

3

這裏有很好的文檔Advanced indexing with hierarchical index

演示:

In [455]: df.loc[(slice(10,20), slice('MSX')), :] 
Out[455]: 
     Value_1 Value_2 Value_3 Value_4 Value_5 
10 MSX 0.407482 0.223845 0.144174 0.434949 0.804687 
20 MSX 0.679788 0.669394 0.099664 0.295965 0.741914 
+0

非常感謝您的回答,但是我的熊貓版本(0.09.0)loc沒有實現。 – nekcorp

相關問題