Python – 从 Pandas 数据框中删除多层级列索引中的多层级
若要从多层级列索引中删除多层级,可重复使用 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'])] # forming multiindex dataframe 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")]) # multiindex array arr = [np.array(['car', 'car', 'car','bike','bike', 'bike', 'truck', 'truck', 'truck']), np.array(['valueA', 'valueB', 'valueC','valueA', 'valueB', 'valueC','valueA', 'valueB', 'valueC'])] # forming multiindex dataframe dataFrame = pd.DataFrame(np.random.randn(9, 3), index=arr,columns=items) # labelling index 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
广告