2014-03-24 130 views
0

我已經導入了一個csv作爲多索引數據框。這裏的數據的樣機:用大熊貓切片Mutliindex數據

df = pd.read_csv("coursedata2.csv", index_col=[0,2])

print (df)

        COURSE 

ID Course List
12345 Interior Environments DESN10000 Rendering & Present Skills DESN20065 Lighting DESN20025 22345 Drawing Techniques DESN10016 Colour Theory DESN14049 Finishes & Sustainable Issues DESN12758 Lighting DESN20025 32345 Window Treatments&Soft Furnish DESN27370 42345 Introduction to CADD INFO16859 Principles of Drafting DESN10065 Drawing Techniques DESN10016 The Fundamentals of Design DESN15436 Colour Theory DESN14049 Interior Environments DESN10000 Drafting DESN1 Textiles and Applications DESN10199 Finishes & Sustainable Issues DESN12758

[17 rows x 1 columns] 

我可以很容易地通過標籤使用.xs切它 - 比如:

     COURSE 
Course List       
Interior Environments  DESN10000 
Rendering & Present Skills DESN20065 
Lighting     DESN20025 

[3 rows x 1 columns] 

>

但是我想做的是通過數據幀的步驟和課程每塊上進行操作,通過ID。真實數據中的ID值是相當隨機的整數,按升序排序。

df.index顯示:

df.index MultiIndex(levels=[[12345, 22345, 32345, 42345], [u'Colour Theory', u'Colour Theory ', u'Drafting', u'Drawing Techniques', u'Finishes & Sustainable Issues', u'Interior Environments', u'Introduction to CADD', u'Lighting', u'Principles of Drafting', u'Rendering & Present Skills', u'Textiles and Applications', u'The Fundamentals of Design', u'Window Treatments&Soft Furnish']], labels=[[0, 0, 0, 1, 1, 1, 1, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3], [5, 9, 7, 3, 1, 4, 7, 12, 6, 8, 3, 11, 0, 5, 2, 10, 4]], names=[u'ID', u'Course List'])

在我看來,我應該能夠使用的第一個索引標籤通過數據幀遞增。 IE瀏覽器。獲取標籤0,然後是1,然後是2,然後是3的所有課程,但它看起來像.xs不會按標籤分片。

我錯過了什麼嗎?

+1

嘗試''df.groupby(level ='ID')。apply(func)',看到這裏:http://pandas.pydata.org/pandas-docs/stable/groupby.html#groupby-with- multiindex – Jeff

回答

0

因此,可能會有更高效的方法來實現這一點,具體取決於您要對數據執行的操作。不過,也有它馬上想到兩種方法:這取決於你是否要對集團整體或每行與它操作

for id_label in df.index.levels[0]: 
    some_func(df.xs(id_label, level='ID')) 

for id_label in df.index.levels[0]: 
    df.xs(id_label, level='ID').apply(some_func, axis=1) 

+0

我想將切片與課程的靜態列表進行比較,所以我認爲第一種方法看起來非常有前途。非常感謝。 – user3455463