官方文档:pandas.Dataframe.drop
Dataframe.drop(labels=None, axis=0, index=None, columns=None, level=None, inplace=False, errors=‘raise’)
labels: 要删除的行标签/列标签axis:默认取0删除行,取1删除列index:删除行(labels, axis=0 is equivalent to index=labels)columns:删除列(labels, axis=1 is equivalent to columns=labels)level:针对有多级行标或列标的集合,level=x 即按照 x 级行/列标删除整行inplace:默认inplace=False, 仅返回copy;inplace=True为在原dataframe上修改errors: {‘ignore’, ‘raise’}, default ‘raise’
Examples:
df = pd.Dataframe(np.arange(12).reshape(3, 4),columns=['A', 'B', 'C', 'D'])# Outputdf A B C D0 0 1 2 31 4 5 6 72 8 9 10 11
1、列删除
# .drop()方法df.drop(columns=['列名1', '列名2'])# ORdf.drop(['列名1', '列名2'], axis=1)# del方法(一次只能删除一列)del df['列名']# .pop()方法(一次只能删除一列)df.pop('列名')
Example:删除 ‘B’ 和 ‘C’ 列
df.drop(['B', 'C'], axis=1)# ORdf.drop(columns=['B', 'C'])# Output A D0 0 31 4 72 8 11
2、行删除 2.1 根据索引删除行
df.drop(['行名1', '行名2'])
Example:删除第0和1行
df.drop([0, 1])# Output A B C D2 8 9 10 11
2.2 根据条件删除行df.drop(df[条件].index)
Example:删除A列大于4的所有行
df.drop(df[df.A >= 4].index)# Output A B C D0 0 1 2 3
3、删除多级行标/列表的Dataframe
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 smalllama speed 45.0 30.0 weight 200.0 100.0 length 1.5 1.0cow speed 30.0 20.0 weight 250.0 150.0 length 1.5 0.8falcon speed 320.0 250.0 weight 1.0 0.8 length 0.3 0.2
3.1 删除特定的索引组合df.drop(index=('索引1', '索引2'))df.drop(index='行名', columns='列名')
Example 1: 删除 ‘falcon’ 的 ‘weight’ 行
df.drop(index=('falcon', 'weight'))# Output big smalllama speed 45.0 30.0 weight 200.0 100.0 length 1.5 1.0cow speed 30.0 20.0 weight 250.0 150.0 length 1.5 0.8falcon speed 320.0 250.0 length 0.3 0.2
Example 2: 删除 ‘cow’ 行和 ‘small’ 列
df.drop(index='cow', columns='small')# Output biglama speed 45.0 weight 200.0 length 1.5falcon speed 320.0 weight 1.0 length 0.3
3.2 按照行/列标删除# 删除第 x+1 级行标, 默认为0df.drop(index='行名', level=x)# 删除第 y+1 级列标,默认为0df.drop(columns='列名', level=y)
Example 1:删除第二级行标的 ‘length’ 行
df.drop(index='length', level=1)# Output big smalllama speed 45.0 30.0 weight 200.0 100.0cow speed 30.0 20.0 weight 250.0 150.0falcon speed 320.0 250.0 weight 1.0 0.8
Example 2:删除第1级行标的 ‘lama’ 行
df.drop(index='lama') # 默认 level=0# Output: big smallcow speed 30.0 20.0 weight 250.0 150.0 length 1.5 0.8falcon speed 320.0 250.0 weight 1.0 0.8 length 0.3 0.2