官方文档:pandas.DataFrame.drop
DataFrame.drop(labels=None, axis=0, index=None, columns=None, level=None, inplace=False, errors=‘raise’)
Examples:
df = pd.DataFrame(np.arange(12).reshape(3, 4),columns=['A', 'B', 'C', 'D'])
# Output
df
A B C D
0 0 1 2 3
1 4 5 6 7
2 8 9 10 11
# .drop()方法
df.drop(columns=['列名1', '列名2'])
# OR
df.drop(['列名1', '列名2'], axis=1)
# del方法(一次只能删除一列)
del df['列名']
# .pop()方法(一次只能删除一列)
df.pop('列名')
Example:删除 ‘B’ 和 ‘C’ 列
df.drop(['B', 'C'], axis=1)
# OR
df.drop(columns=['B', 'C'])
# Output
A D
0 0 3
1 4 7
2 8 11
df.drop(['行名1', '行名2'])
Example:删除第0和1行
df.drop([0, 1])
# Output
A B C D
2 8 9 10 11
df.drop(df[条件].index)
Example:删除A列大于4的所有行
df.drop(df[df.A >= 4].index)
# Output
A B C D
0 0 1 2 3
Example
midx = pd.MultiIndex(levels=[['speed', 'cow', 'falcon'],
['speed', 'weight', 'length']],
codes=[[0, 0, 0, 1, 1, 1, 2, 2, 2],
[0, 1, 2, 0, 1, 2, 0, 1, 2]])
df = pd.DataFrame(index=midx, columns=['big', 'small'],
data=[[45, 30], [200, 100], [1.5, 1], [30, 20],
[250, 150], [1.5, 0.8], [320, 250],
[1, 0.8], [0.3,0.2]])
# Output
big small
lama speed 45.0 30.0
weight 200.0 100.0
length 1.5 1.0
cow speed 30.0 20.0
weight 250.0 150.0
length 1.5 0.8
falcon speed 320.0 250.0
weight 1.0 0.8
length 0.3 0.2
df.drop(index=('索引1', '索引2'))
df.drop(index='行名', columns='列名')
Example 1: 删除 ‘falcon’ 的 ‘weight’ 行
df.drop(index=('falcon', 'weight'))
# Output
big small
lama speed 45.0 30.0
weight 200.0 100.0
length 1.5 1.0
cow speed 30.0 20.0
weight 250.0 150.0
length 1.5 0.8
falcon speed 320.0 250.0
length 0.3 0.2
Example 2: 删除 ‘cow’ 行和 ‘small’ 列
df.drop(index='cow', columns='small')
# Output
big
lama speed 45.0
weight 200.0
length 1.5
falcon speed 320.0
weight 1.0
length 0.3
# 删除第 x+1 级行标, 默认为0
df.drop(index='行名', level=x)
# 删除第 y+1 级列标,默认为0
df.drop(columns='列名', level=y)
Example 1:删除第二级行标的 ‘length’ 行
df.drop(index='length', level=1)
# Output
big small
lama speed 45.0 30.0
weight 200.0 100.0
cow speed 30.0 20.0
weight 250.0 150.0
falcon speed 320.0 250.0
weight 1.0 0.8
Example 2:删除第1级行标的 ‘lama’ 行
df.drop(index='lama') # 默认 level=0
# Output:
big small
cow speed 30.0 20.0
weight 250.0 150.0
length 1.5 0.8
falcon speed 320.0 250.0
weight 1.0 0.8
length 0.3 0.2