我导入了一个csv作为多索引数据帧。以下是数据的模型:
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 DESN10123
Textiles and Applications DESN10199
Finishes & Sustainable Issues DESN12758
我可以使用.xs轻松地按标签对其进行切片--例如:
selected = df.xs (12345, level='ID')
print selected
COURSE
Course List
Interior Environments DESN10000
Rendering & Present Skills DESN20065
Lighting DESN20025
[3 rows x 1 columns]
>
但我想做的是逐步通过数据帧,并对每一块课程按ID执行一次操作。实际数据中的ID值是相当随机的整数,按升序排序。在
在测向索引显示:
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'])
在我看来,我应该能够使用第一个索引标签在数据帧中递增。也就是说,获得标签0的所有课程,然后是1,然后是2,然后是3,。。。但看起来.xs不会按标签进行切片。在
我错过什么了吗?在
因此,可能有更有效的方法来实现这一点,这取决于您试图对数据做什么。然而,有两种方法可以立即想到:
以及
^{pr2}$这取决于您是要将组作为一个整体来操作,还是对组中的每一行进行操作。在
相关问题 更多 >
编程相关推荐