python数据处理(1)
1.删除重复值
# 删除重复值
data=pd.DataFrame({'k1':['one','two']*3+['two'],'k2':[1,1,2,3,3,4,4]})
print(data)
data_1=data.drop_duplicates()
print(data_1)
结果输出
k1 k2
0 one 1
1 two 1
2 one 2
3 two 3
4 one 3
5 two 4
6 two 5
k1 k2
0 one 1
1 two 1
2 one 2
3 two 3
4 one 3
5 two 4
2.数据转换
data=pd.DataFrame({'food':['bacon','pulled pock','bacon','pastrami','corned beef','Bacon','pastrami','honey ham','nova lox'],
'ounces':[4,3,12,6,7.5,8,3,5,6]})
print(data)
food ounces
0 bacon 4.0
1 pulled pock 3.0
2 bacon 12.0
3 pastrami 6.0
4 corned beef 7.5
5 Bacon 8.0
6 pastrami 3.0
7 honey ham 5.0
8 nova lox 6.0
meat_to_animal={
'bacon':'pig',
'pulled pock':'pig',
'pastrami':'cow',
'corned beef':'cow',
'honey ham':'pig',
'nova lox':'salmon'
}
lowercased=data['food'].str.lower()
print(lowercased)
data['animal']=lowercased.map(meat_to_animal)
print(data)
map()不仅可以使函数映射,还可以就字符串和字符串一一对应拓开
0 bacon
1 pulled pock
2 bacon
3 pastrami
4 corned beef
5 bacon
6 pastrami
7 honey ham
8 nova lox
Name: food, dtype: object
#####################
food ounces animal
0 bacon 4.0 pig
1 pulled pock 3.0 pig
2 bacon 12.0 pig
3 pastrami 6.0 cow
4 corned beef 7.5 cow
5 Bacon 8.0 pig
6 pastrami 3.0 cow
7 honey ham 5.0 pig
8 nova lox 6.0 salmon
3.替代值
ffillna填充缺失值是通用值替换的特殊案列
map可以用来修改一个对象中子集的值
但是replace提供了更为简单灵活的实现
data=pd.Series([1.,-999.,2.,-999.,-1000.,3.])
print(data)
dd=data.replace(-999,np.nan)
print(dd)
dp=data.replace([-999,-1000],np.nan)
print(dp)
dg=data.replace([-999,-1000],[np.nan,0])
print(dg)
ds=data.replace({-999:np.nan,-1000:0})
print(ds)
data.replace 和data.str.replace这两个方法是不同的,后者是对字符串按元素替代的。