目录
一、基本知识
二、具体实例
三、注意事项
四、知识延申
总结
pandas中的del函数能够删除特定的列。
具体的用法如下:
del
df[
'A'
]
# 删除A列,会就地修改
示例:先增添一个由布尔值组成的列:
import pandas as pd
import numpy as np
data = {'state': ['Astrilia', 'Mexico', 'China', 'Mexico'],
'years': [2000, 2001, 2002, 2003],
'pop': [1.5, 3.6, 2.4, 5.1]}
frame = pd.DataFrame(data, columns = ['years', 'state', 'pop'])
val = pd.Series([-1.2, -1.5, -1.7])
frame['debt'] = val
val_1 = pd.Series([100, 200, 300], index = [0, 1, 3])
frame['pofit'] = val_1
print(frame)
'''现在构建一个布尔值组成的数组,如果state == Mexico,则在FT列输出T,否则为F'''
#方法1
frame['TF'] = frame.state == 'Mexico'
print(frame)
print(frame.TF[0])
#方法2
Buer = []
for i in range(4):
Buer.append(frame.state[i] == 'Mexico')
frame['tf'] = Buer
print(frame)
结果:
years state pop debt pofit
0 2000 Astrilia 1.5 -1.2 100.0
1 2001 Mexico 3.6 -1.5 200.0
2 2002 China 2.4 -1.7 NaN
3 2003 Mexico 5.1 NaN 300.0
years state pop debt pofit TF
0 2000 Astrilia 1.5 -1.2 100.0 False
1 2001 Mexico 3.6 -1.5 200.0 True
2 2002 China 2.4 -1.7 NaN False
3 2003 Mexico 5.1 NaN 300.0 True
False
years state pop debt pofit TF tf
0 2000 Astrilia 1.5 -1.2 100.0 False False
1 2001 Mexico 3.6 -1.5 200.0 True True
2 2002 China 2.4 -1.7 NaN False False
3 2003 Mexico 5.1 NaN 300.0 True True
进程已结束,退出代码0
然后删除TF列:
import pandas as pd
import numpy as np
data = {'state': ['Astrilia', 'Mexico', 'China', 'Mexico'],
'years': [2000, 2001, 2002, 2003],
'pop': [1.5, 3.6, 2.4, 5.1]}
frame = pd.DataFrame(data, columns = ['years', 'state', 'pop'])
val = pd.Series([-1.2, -1.5, -1.7])
frame['debt'] = val
val_1 = pd.Series([100, 200, 300], index = [0, 1, 3])
frame['pofit'] = val_1
print(frame)
'''现在构建一个布尔值组成的数组,如果state == Mexico,则在FT列输出T,否则为F'''
#构建一个新的列
frame['TF'] = frame.state == 'Mexico'
print(frame)
#删除该列
del frame['TF']
print(frame)
结果:
years state pop debt pofit
0 2000 Astrilia 1.5 -1.2 100.0
1 2001 Mexico 3.6 -1.5 200.0
2 2002 China 2.4 -1.7 NaN
3 2003 Mexico 5.1 NaN 300.0
years state pop debt pofit TF
0 2000 Astrilia 1.5 -1.2 100.0 False
1 2001 Mexico 3.6 -1.5 200.0 True
2 2002 China 2.4 -1.7 NaN False
3 2003 Mexico 5.1 NaN 300.0 True
years state pop debt pofit
0 2000 Astrilia 1.5 -1.2 100.0
1 2001 Mexico 3.6 -1.5 200.0
2 2002 China 2.4 -1.7 NaN
3 2003 Mexico 5.1 NaN 300.0
进程已结束,退出代码0
注意这里:
必须要这样写,才可以正常跑通
如果写成这样:
del删除之后,原数据改变,如果不想让原数据改变,可以用drop
具体分析如下:
del s['d'] #序列删除;原数据改变 # del s1.d #错误用法
s.pop('d') #弹出;参数必须为标签str;原数据改变
s.drop('d') #删除数据‘d' ; 原数据不变
s.drop(['c','d']) #删除数据‘'c',d'
示例:
import pandas as pd
import numpy as np
data = {'state': ['Astrilia', 'Mexico', 'China', 'Mexico'],
'years': [2000, 2001, 2002, 2003],
'pop': [1.5, 3.6, 2.4, 5.1]}
frame = pd.DataFrame(data, columns = ['years', 'state', 'pop'])
val = pd.Series([-1.2, -1.5, -1.7])
frame['debt'] = val
val_1 = pd.Series([100, 200, 300], index = [0, 1, 3])
frame['pofit'] = val_1
print(frame)
'''现在构建一个布尔值组成的数组,如果state == Mexico,则在FT列输出T,否则为F'''
#构建一个新的列
frame['TF'] = frame.state == 'Mexico'
#删除该列
print(frame.drop(['TF'],axis=1))#可以看出,删了,但没完全删,哈哈哈啊哈
print(frame)
结果:
years state pop debt pofit
0 2000 Astrilia 1.5 -1.2 100.0
1 2001 Mexico 3.6 -1.5 200.0
2 2002 China 2.4 -1.7 NaN
3 2003 Mexico 5.1 NaN 300.0
years state pop debt pofit TF
0 2000 Astrilia 1.5 -1.2 100.0 False
1 2001 Mexico 3.6 -1.5 200.0 True
2 2002 China 2.4 -1.7 NaN False
3 2003 Mexico 5.1 NaN 300.0 True
years state pop debt pofit
0 2000 Astrilia 1.5 -1.2 100.0
1 2001 Mexico 3.6 -1.5 200.0
2 2002 China 2.4 -1.7 NaN
3 2003 Mexico 5.1 NaN 300.0
years state pop debt pofit TF
0 2000 Astrilia 1.5 -1.2 100.0 False
1 2001 Mexico 3.6 -1.5 200.0 True
2 2002 China 2.4 -1.7 NaN False
3 2003 Mexico 5.1 NaN 300.0 True
注意drop的用法:
类似于append
del很简单,但是一定要注意使用的格式!谨记!