📌  相关文章
📜  如何从 Pandas Dataframe 中的多级列索引中删除一个级别?

📅  最后修改于: 2022-05-13 01:55:48.768000             🧑  作者: Mango

如何从 Pandas Dataframe 中的多级列索引中删除一个级别?

在本文中,我们将学习如何从多级列索引中删除一个级别。但在此之前,我们需要了解什么是多级索引。多级索引数据框是一种包含多级或分层索引的数据框。
在本文中,我们将创建一个我们自己选择的具有多列索引的数据框,然后我们将降低层次索引的级别。

分步实施

让我们在示例的帮助下使用分步实现来理解这一点。

第 1 步:导入所有需要的库。

Python3
# importing all important libraries
import pandas as pd


Python3
# Creating a multilevel index 
index = pd.MultiIndex.from_tuples([("Group 1", "Group 1"), 
                                   ("Group 1", "Group 2"),
                                   ("Group 3","Group 3")])
  
# Creating a pandas dataframe with 
# multilevel-column indexing
df = pd.DataFrame([["Ross","Joey","Chandler"],
                   ["Rachel","","Monica"]], 
                  columns=index)
  
# Labelling the dataframe index.
index = df. index
index. name = "F.R.I.E.N.D.S"
  
# Showing the above multi-index column
# dataframe
print(df)


Python3
# Dropping a level down
df.columns = df.columns.droplevel(0)


Python3
print(df)


Python3
# importing all important libraries
import pandas as pd
  
# Creating a multilevel index 
index = pd.MultiIndex.from_tuples([("Company A", "Company B","Company C"), 
                                   ("Company A", "Company A","Company B"),
                                   ("Company A","Company B","Company C")])
  
# Creating a pandas dataframe with 
# multilevel-column indexing
df = pd.DataFrame([["Atreyi","Digangana","Sohom"],
                   ["Sujit","Bjon","Rajshekhar"],
                   ["Debosmita","Shatabdi",""]], 
                  columns=index)
  
# Labelling the dataframe index.
index = df. index
index. name = "ECE Placement"
  
# Showing the above multi-index column
# dataframe
print(df)


Python3
# Dropping a level number 2
df.columns = df.columns.droplevel(2)
print(df)


Python3
# importing all important libraries
import pandas as pd
  
# Creating a multilevel index
index = pd.MultiIndex.from_tuples([("Company A", "Company B", "Company C"),
                                   ("Company A", "Company A", "Company B"),
                                   ("Company A", "Company B", "Company C")])
  
# Creating a pandas dataframe with
# multilevel-column indexing
df = pd.DataFrame([["Atreyi", "Digangana", "Sohom"],
                   ["Sujit", "Bjon", "Rajshekhar"],
                   ["Debosmita", "Shatabdi", ""]],
                  columns=index)
  
# Labelling the dataframe index.
index = df. index
index. name = "ECE Placement"
  
# Showing the above multi-index column
# dataframe
print(df)


Python3
# Dropping a level down
df.columns = df.columns.droplevel(0)
  
# Dropping another level down
df.columns = df.columns.droplevel(0)
  
# Showing the dataframe
print(df)


Python3
# importing all important libraries
import pandas as pd
  
# Creating a pandas dataframe
df = pd.DataFrame([["Coding", "System Design"],
                   ["DBMS", "Aptitude"],
                   ["Logical Reasoning", "Development"]])
  
# Creating multilevel index from tuples
df.columns = pd.MultiIndex.from_tuples([('Group 1', 'Group 2', 'Group 3', 'Group 4'),
                                        ('Group 3', 'Group 4', 'Group 5', 'Group 6')],
                                       names=['level 1', 'level 2', 'level 3', 'level 4'])
# Showing the dataframe
print(df)


Python3
# Dropping a level down(Level 1)
df.columns = df.columns.droplevel(0)
  
# Dropping a level down after
# re-arrangement(Level 2)
df.columns = df.columns.droplevel(1)
  
# Showing the dataframe
print(df)


第 2 步:创建一个多级列索引 Pandas Dataframe 并显示它。



我们正在使用MultiIndex.from_tuples()创建一个多索引列 这有助于我们创建多个索引,一个下一个,并且它是按列创建的。之后,我们使用 pd.Dataframe() 创建数据并将其转换为表格格式,以列名作为多级索引。此外,我们正在使用 df.index 更改表的索引名称。

蟒蛇3

# Creating a multilevel index 
index = pd.MultiIndex.from_tuples([("Group 1", "Group 1"), 
                                   ("Group 1", "Group 2"),
                                   ("Group 3","Group 3")])
  
# Creating a pandas dataframe with 
# multilevel-column indexing
df = pd.DataFrame([["Ross","Joey","Chandler"],
                   ["Rachel","","Monica"]], 
                  columns=index)
  
# Labelling the dataframe index.
index = df. index
index. name = "F.R.I.E.N.D.S"
  
# Showing the above multi-index column
# dataframe
print(df)

输出:

第 3 步:删除数据帧的级别

现在使用Python创建了一个多级列索引数据框。现在让我们实现上面的概念。我们需要降低一个级别。我们可以使用df.columns.droplevel(level=0)来做到这一点。这有助于我们从索引 0 的顶部降低索引级别。

蟒蛇3

# Dropping a level down
df.columns = df.columns.droplevel(0)

第 4 步:显示所需的结果



蟒蛇3

print(df)

输出:

因此,我们已经能够成功删除索引列的级别。

让我们看一些基于上述方法的更多示例。

示例 1:

在下一个示例中,我们将从多级列索引中的特定索引中删除一个级别。这可以使用我们之前使用的相同语法来完成[ df.columns.droplevel(level=0) ] 如果我们指定级别编号,则根据从零开始的索引删除以下索引。那么让我们开始实施这个概念。

蟒蛇3

# importing all important libraries
import pandas as pd
  
# Creating a multilevel index 
index = pd.MultiIndex.from_tuples([("Company A", "Company B","Company C"), 
                                   ("Company A", "Company A","Company B"),
                                   ("Company A","Company B","Company C")])
  
# Creating a pandas dataframe with 
# multilevel-column indexing
df = pd.DataFrame([["Atreyi","Digangana","Sohom"],
                   ["Sujit","Bjon","Rajshekhar"],
                   ["Debosmita","Shatabdi",""]], 
                  columns=index)
  
# Labelling the dataframe index.
index = df. index
index. name = "ECE Placement"
  
# Showing the above multi-index column
# dataframe
print(df)

输出:



现在,如果我们想降低索引为 2 的级别,那么让我们看看会发生什么!

蟒蛇3

# Dropping a level number 2
df.columns = df.columns.droplevel(2)
print(df)

输出:

因此,我们可以观察到,在多级列索引中,我们成功删除了索引号为 2 的级别。

示例 2:

在这个例子中,我们将实现多级索引的更多概念。我们将同时删除多个级别。

蟒蛇3

# importing all important libraries
import pandas as pd
  
# Creating a multilevel index
index = pd.MultiIndex.from_tuples([("Company A", "Company B", "Company C"),
                                   ("Company A", "Company A", "Company B"),
                                   ("Company A", "Company B", "Company C")])
  
# Creating a pandas dataframe with
# multilevel-column indexing
df = pd.DataFrame([["Atreyi", "Digangana", "Sohom"],
                   ["Sujit", "Bjon", "Rajshekhar"],
                   ["Debosmita", "Shatabdi", ""]],
                  columns=index)
  
# Labelling the dataframe index.
index = df. index
index. name = "ECE Placement"
  
# Showing the above multi-index column
# dataframe
print(df)

输出:



正如我们所看到的,每个数组列表都包含按列的索引。因此,三个数组意味着三列,数组中的值数是指行数。现在让我们从数据框中删除多个索引。我们可以通过多次调用df.columns.droplevel(level=0)来做到这一点。但这里有一个问题!

蟒蛇3

# Dropping a level down
df.columns = df.columns.droplevel(0)
  
# Dropping another level down
df.columns = df.columns.droplevel(0)
  
# Showing the dataframe
print(df)

如我们所见,有两个droplevel语句的级别为 0。这是因为在删除单个级别后,其余的会重新排列。因此,位于索引 1 的级别现在将变为索引 0,因此在这种情况下会写入多个 droplevel。

输出:

因此,级别 0 和级别 1 被删除,我们只剩下级别 2,现在显示为级别 0。

示例 3:

在最后一个示例中,让我们从数据框中的各个位置删除多个级别。

蟒蛇3

# importing all important libraries
import pandas as pd
  
# Creating a pandas dataframe
df = pd.DataFrame([["Coding", "System Design"],
                   ["DBMS", "Aptitude"],
                   ["Logical Reasoning", "Development"]])
  
# Creating multilevel index from tuples
df.columns = pd.MultiIndex.from_tuples([('Group 1', 'Group 2', 'Group 3', 'Group 4'),
                                        ('Group 3', 'Group 4', 'Group 5', 'Group 6')],
                                       names=['level 1', 'level 2', 'level 3', 'level 4'])
# Showing the dataframe
print(df)

输出:



现在让我们分别删除级别 1 和 3:

蟒蛇3

# Dropping a level down(Level 1)
df.columns = df.columns.droplevel(0)
  
# Dropping a level down after
# re-arrangement(Level 2)
df.columns = df.columns.droplevel(1)
  
# Showing the dataframe
print(df)

正如我们所见,在第一种情况下,我们从索引 0 下降了一个级别。重新排列后,第 2 级现在将变为多级索引数据帧的 0 个索引。现在为了删除3级,我们必须根据重新排列后的基于0的索引将级别指定为1。现在级别 2 和 4 将显示在结果输出中。

输出: