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
posted @ 2022-08-19 23:00  luoganttcc  阅读(20)  评论(0编辑  收藏  举报