pandas Series DataFrame 丢弃指定轴上的项(三)

1.0 删除Series项

from pandas import Series,DataFrame
import numpy as np
import pandas as pd
obj=Series(np.arange(5),index=['a','b','c','d','e'])

obj
Out[82]: 
a    0
b    1
c    2
d    3
e    4
dtype: int32
new_obj=obj.drop('c')

new_obj
Out[84]: 
a    0
b    1
d    3
e    4
dtype: int32
obj=Series(np.arange(5),index=['a','b','c','d','e'])

obj
Out[90]: 
a    0
b    1
c    2
d    3
e    4
dtype: int32
obj.drop(['d','c'])
Out[91]: 
a    0
b    1
e    4
dtype: int32

2.0 删除DataFrame项

 data=DataFrame(np.arange(16).reshape((4,4)),index=['ohio','colorado','utah','new york'],columns=['one','two','three','four'])

data
Out[94]: 
          one  two  three  four
ohio        0    1      2     3
colorado    4    5      6     7
utah        8    9     10    11
new york   12   13     14    15

     删除 colorado 和ohio

data.drop(['colorado','ohio'])
Out[95]: 
          one  two  three  four
utah        8    9     10    11
new york   12   13     14    15
data.drop('two',axis=1)
Out[98]: 
          one  three  four
ohio        0      2     3
colorado    4      6     7
utah        8     10    11
new york   12     14    15
data.drop(['two','four'],axis=1)
Out[100]: 
          one  three
ohio        0      2
colorado    4      6
utah        8     10
new york   12     14
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值