Python - 从 Pandas 数据框中的多级列索引中删除多个级别
要从多级列索引中删除多个级别,请重复使用。我们已经使用isused来按列创建索引。columns.droplevel()Multiindex.from_tuples()
首先,按列创建索引-
items = pd.MultiIndex.from_tuples([("Col 1", "Col 1", "Col 1"),("Col 2", "Col 2", "Col 2"),("Col 3", "Col 3", "Col 3")])
接下来,创建一个多索引数组并形成一个多索引数据帧-
arr = [np.array(['car', 'car', 'car','bike','bike', 'bike', 'truck', 'truck', 'truck']), np.array(['valueA', 'valueB', 'valueC','valueA', 'valueB', 'valueC','valueA', 'valueB', 'valueC'])] #形成多索引数据帧 dataFrame = pd.DataFrame(np.random.randn(9, 3), index=arr,columns=items)
标记索引-
dataFrame.index.names = ['level 0', 'level 1']
在索引0处下降一个级别-
dataFrame.columns = dataFrame.columns.droplevel(0)
我们在0索引处删除了一个级别。删除后,1级现在是0级。要删除另一个级别,只需再次使用上面的即
dataFrame.columns = dataFrame.columns.droplevel(0)
以下是代码
示例
import numpy as np import pandas as pd items = pd.MultiIndex.from_tuples([("Col 1", "Col 1", "Col 1"),("Col 2", "Col 2", "Col 2"),("Col 3", "Col 3", "Col 3")]) #多索引数组 arr = [np.array(['car', 'car', 'car','bike','bike', 'bike', 'truck', 'truck', 'truck']), np.array(['valueA', 'valueB', 'valueC','valueA', 'valueB', 'valueC','valueA', 'valueB', 'valueC'])] #形成多索引数据帧 dataFrame = pd.DataFrame(np.random.randn(9, 3), index=arr,columns=items) #标签索引 dataFrame.index.names = ['one', 'two'] print"DataFrame...\n",dataFrame print"\nDropping a level...\n"; dataFrame.columns = dataFrame.columns.droplevel(0) print"Updated DataFrame..\n",dataFrame print"\nDropping another level...\n"; dataFrame.columns = dataFrame.columns.droplevel(0) print"Updated DataFrame..\n",dataFrame输出结果
这将产生以下输出-
DataFrame... Col 1 Col 2 Col 3 Col 1 Col 2 Col 3 Col 1 Col 2 Col 3 one two car valueA 0.425077 0.020606 1.148156 valueB -1.720355 0.502863 1.184753 valueC 0.373106 1.300935 -0.128404 bike valueA -0.648708 0.944725 0.593327 valueB -0.613921 -0.238730 -0.218448 valueC 0.313042 -0.628065 0.910935 truck valueA 0.286377 0.478067 -1.000645 valueB 1.151793 -0.171433 -0.612346 valueC -1.358061 0.735075 0.092700 Dropping a level... Updated DataFrame.. Col 1 Col 2 Col 3 Col 1 Col 2 Col 3 one two car valueA 0.425077 0.020606 1.148156 valueB -1.720355 0.502863 1.184753 valueC 0.373106 1.300935 -0.128404 bike valueA -0.648708 0.944725 0.593327 valueB -0.613921 -0.238730 -0.218448 valueC 0.313042 -0.628065 0.910935 truck valueA 0.286377 0.478067 -1.000645 valueB 1.151793 -0.171433 -0.612346 valueC -1.358061 0.735075 0.092700 Dropping another level... Updated DataFrame.. Col 1 Col 2 Col 3 one two car valueA 0.425077 0.020606 1.148156 valueB -1.720355 0.502863 1.184753 valueC 0.373106 1.300935 -0.128404 bike valueA -0.648708 0.944725 0.593327 valueB -0.613921 -0.238730 -0.218448 valueC 0.313042 -0.628065 0.910935 truck valueA 0.286377 0.478067 -1.000645 valueB 1.151793 -0.171433 -0.612346 valueC -1.358061 0.735075 0.092700