按标签名称或按索引位置在DataFrame中删除列
在这篇文章中,我们将讨论如何在Pandas Dataframe中通过标签名称或索引位置来删除列。从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'])
# show the dataframe
details
Python
输出 :
**方法1:使用dataframe.drop()方法从数据框架中删除列。
**例子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
Python
输出 :
例子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
Python
输出 :
例子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
Python
输出 :
方法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
Python
输出 :
**方法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
Python
输出 :
注意:不同的loc()和iloc()是iloc()不包括最后一列范围元素。
方法4:通过迭代的方式从数据框架中删除列。
例子:删除特定的列。
# 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
Python
输出 :