2014-03-24 68 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