📜  通过标签名称或索引位置删除 DataFrame 中的列

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

通过标签名称或索引位置删除 DataFrame 中的列

在本文中,我们将讨论如何通过标签名称或索引位置删除 Pandas Dataframe 中的列。从 DataFrame 中删除列可以通过多种方式实现。

让我们用一个列表字典创建一个简单的数据框,比如列名是:'Name'、'Age'、'Place'、'College'。

# import pandas library as pd
import pandas as pd
  
# List of Tuples
students = [('Ankit', 22, 'Up', 'Geu'),
           ('Ankita', 31, 'Delhi', 'Gehu'),
           ('Rahul', 16, 'Tokyo', 'Abes'),
           ('Simran', 41, 'Delhi', 'Gehu'),
           ('Shaurya', 33, 'Delhi', 'Geu'),
           ('Harshita', 35, 'Mumbai', 'Bhu' ),
           ('Swapnil', 35, 'Mp', 'Geu'),
           ('Priya', 35, 'Uk', 'Geu'),
           ('Jeet', 35, 'Guj', 'Gehu'),
           ('Ananya', 35, 'Up', 'Bhu')
            ]
  
# Create a DataFrame object from
# list of tuples with columns
# and indices.
details = pd.DataFrame(students,
                       columns =['Name', 'Age', 
                                 'Place', 'College'],
                        index =['a', 'b', 'c', 'd', 'e', 
                                'f', 'g', 'i', 'j', 'k'])
# show the dataframe
details

输出 :
数据帧图像

方法 1:使用 dataframe.drop dataframe.drop()方法从 Dataframe 中删除列。
示例 1:删除特定的单一提及列。

# import pandas library as pd
import pandas as pd
  
# List of Tuples
students = [('Ankit', 22, 'Up', 'Geu'),
           ('Ankita', 31, 'Delhi', 'Gehu'),
           ('Rahul', 16, 'Tokyo', 'Abes'),
           ('Simran', 41, 'Delhi', 'Gehu'),
           ('Shaurya', 33, 'Delhi', 'Geu'),
           ('Harshita', 35, 'Mumbai', 'Bhu' ),
           ('Swapnil', 35, 'Mp', 'Geu'),
           ('Priya', 35, 'Uk', 'Geu'),
           ('Jeet', 35, 'Guj', 'Gehu'),
           ('Ananya', 35, 'Up', 'Bhu')
            ]
  
# Create a DataFrame object from
# list of tuples with columns
# and indices.
details = pd.DataFrame(students, 
                       columns =['Name', 'Age',
                                 'Place', 'College'],
                       index =['a', 'b', 'c', 'd', 'e',
                               'f', 'g', 'i', 'j', 'k'])
  
# Remove column name 'Age' 
rslt_df = details.drop(['Age'],
                       axis = 1)
# show the dataframe
rslt_df

输出 :
从数据框中删除列 - 1

示例 2:删除特定的多次提及列。

# import pandas library as pd
import pandas as pd
  
# List of Tuples
students = [('Ankit', 22, 'Up', 'Geu'),
           ('Ankita', 31, 'Delhi', 'Gehu'),
           ('Rahul', 16, 'Tokyo', 'Abes'),
           ('Simran', 41, 'Delhi', 'Gehu'),
           ('Shaurya', 33, 'Delhi', 'Geu'),
           ('Harshita', 35, 'Mumbai', 'Bhu' ),
           ('Swapnil', 35, 'Mp', 'Geu'),
           ('Priya', 35, 'Uk', 'Geu'),
           ('Jeet', 35, 'Guj', 'Gehu'),
           ('Ananya', 35, 'Up', 'Bhu')
            ]
  
# Create a DataFrame object from
# list of tuples with columns
# and indices.
details = pd.DataFrame(students, 
                       columns =['Name', 'Age', 
                                 'Place', 'College'],
                       index =['a', 'b', 'c', 'd', 'e',
                               'f', 'g', 'i', 'j', 'k'])
  
# Remove two columns name is 'Age' and 
# 'College' 
rslt_df = details.drop(['Age', 'College'],
                       axis = 1)
# show the dataframe
rslt_df

输出 :
从数据框中删除列 - 2

示例 3:根据列索引删除列。

# import pandas library as pd
import pandas as pd
  
# List of Tuples
students = [('Ankit', 22, 'Up', 'Geu'),
           ('Ankita', 31, 'Delhi', 'Gehu'),
           ('Rahul', 16, 'Tokyo', 'Abes'),
           ('Simran', 41, 'Delhi', 'Gehu'),
           ('Shaurya', 33, 'Delhi', 'Geu'),
           ('Harshita', 35, 'Mumbai', 'Bhu' ),
           ('Swapnil', 35, 'Mp', 'Geu'),
           ('Priya', 35, 'Uk', 'Geu'),
           ('Jeet', 35, 'Guj', 'Gehu'),
           ('Ananya', 35, 'Up', 'Bhu')
            ]
  
# Create a DataFrame object from
# list of tuples with columns
# and indices.
details = pd.DataFrame(students,
                       columns =['Name', 'Age', 
                                 'Place', 'College'],
                       index =['a', 'b', 'c', 'd', 'e',
                               'f', 'g', 'i', 'j', 'k'])
  
# Remove three columns as index base
# 0, 1, 2
rslt_df = details.drop(details.columns[[0, 1, 2]],
                       axis = 1)
  
# show the dataframe
rslt_df

输出 :
从数据框中删除列 - 3

方法 2:使用iloc[]drop()方法从数据框中删除列。

示例:删除特定列到另一列之间的所有列(排除)

# import pandas library as pd
import pandas as pd
  
# List of Tuples
students = [('Ankit', 22, 'Up', 'Geu'),
           ('Ankita', 31, 'Delhi', 'Gehu'),
           ('Rahul', 16, 'Tokyo', 'Abes'),
           ('Simran', 41, 'Delhi', 'Gehu'),
           ('Shaurya', 33, 'Delhi', 'Geu'),
           ('Harshita', 35, 'Mumbai', 'Bhu' ),
           ('Swapnil', 35, 'Mp', 'Geu'),
           ('Priya', 35, 'Uk', 'Geu'),
           ('Jeet', 35, 'Guj', 'Gehu'),
           ('Ananya', 35, 'Up', 'Bhu')
            ]
  
# Create a DataFrame object from
# list of tuples with columns
# and indices.
details = pd.DataFrame(students, 
                       columns =['Name', 'Age', 
                                 'Place', 'College'],
                       index =['a', 'b', 'c', 'd', 'e', 
                               'f', 'g', 'i', 'j', 'k'])
  
# Remove all columns from column
# index 1 to 3(exclude) 
rslt_df = details.drop(details.iloc[:, 1:3],
                       axis = 1)
  
# show the dataframe
rslt_df

输出 :
从数据框中删除列 - 4

方法 3:使用loc[]drop()方法从数据框中删除列。
示例:删除特定列名到另一个列名之间的所有列。

# import pandas library as pd
import pandas as pd
  
# List of Tuples
students = [('Ankit', 22, 'Up', 'Geu'),
           ('Ankita', 31, 'Delhi', 'Gehu'),
           ('Rahul', 16, 'Tokyo', 'Abes'),
           ('Simran', 41, 'Delhi', 'Gehu'),
           ('Shaurya', 33, 'Delhi', 'Geu'),
           ('Harshita', 35, 'Mumbai', 'Bhu' ),
           ('Swapnil', 35, 'Mp', 'Geu'),
           ('Priya', 35, 'Uk', 'Geu'),
           ('Jeet', 35, 'Guj', 'Gehu'),
           ('Ananya', 35, 'Up', 'Bhu')
            ]
  
# Create a DataFrame object from
# list of tuples with columns
# and indices.
details = pd.DataFrame(students, 
                       columns =['Name', 'Age', 
                                 'Place', 'College'],
                       index =['a', 'b', 'c', 'd', 'e', 
                               'f', 'g', 'i', 'j', 'k'])
  
# Remove all columns from column name 
# 'Name' to 'College' 
rslt_df = details.drop(details.loc[:, 'Name':'College'].columns,
                       axis = 1)
  
# show the dataframe
# only indexes print
rslt_df

输出 :
从数据框中删除列 - 6

注意:不同的loc()iloc()是 iloc() 排除最后一列范围元素。

方法 4:通过迭代方式从 Dataframe 中删除列。
示例:删除特定列。

# import pandas library as pd
import pandas as pd
  
# List of Tuples
students = [('Ankit', 22, 'Up', 'Geu'),
           ('Ankita', 31, 'Delhi', 'Gehu'),
           ('Rahul', 16, 'Tokyo', 'Abes'),
           ('Simran', 41, 'Delhi', 'Gehu'),
           ('Shaurya', 33, 'Delhi', 'Geu'),
           ('Harshita', 35, 'Mumbai', 'Bhu' ),
           ('Swapnil', 35, 'Mp', 'Geu'),
           ('Priya', 35, 'Uk', 'Geu'),
           ('Jeet', 35, 'Guj', 'Gehu'),
           ('Ananya', 35, 'Up', 'Bhu')
            ]
  
# Create a DataFrame object from
# list of tuples with columns
# and indices.
details = pd.DataFrame(students, 
                       columns =['Name', 'Age', 
                                 'Place', 'College'],
                       index =['a', 'b', 'c', 'd', 'e',
                               'f', 'g', 'i', 'j', 'k'])
  
# loop throughout all the columns
for column in details.columns :
    if column == 'Age' :
          
        # delete the column
        del details[column]
          
# show the dataframe
details

输出 :
从数据框中删除列 - 7