pandas行/列删除

pandas.DataFrame.drop()函数介绍

官方文档: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'])
# Output
df
   A  B   C   D
0  0  1   2   3
1  4  5   6   7
2  8  9  10  11

1. 列删除

# .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

2. 行删除

2.1 根据索引删除行

df.drop(['行名1', '行名2'])

Example:删除第0和1行

df.drop([0, 1])
# Output
   A  B   C   D
2  8  9  10  11

2.2 根据条件删除行

df.drop(df[条件].index)

Example:删除A列大于4的所有行

df.drop(df[df.A >= 4].index)
# Output
   A  B   C   D
0  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     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

3.1 删除特定的索引组合

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

3.2 按照行/列标删除

# 删除第 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
  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值