Pandas:值替换--replace

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/bqw18744018044/article/details/79963942
import numpy as np
import pandas as pd
from pandas import Series,DataFrame

一、replace的使用

s = Series([1,2,3,4,5,6])
s.replace(6,100)
0      1
1      2
2      3
3      4
4      5
5    100
dtype: int64

二、替换多个值

s.replace([1,2,3,4],100)
0    100
1    100
2    100
3    100
4      5
5      6
dtype: int64

三、不同的值进行不同的替换

s.replace([1,2,3,4],[-1,-2,-3,-4])
0   -1
1   -2
2   -3
3   -4
4    5
5    6
dtype: int64

字典的方式

s.replace({1:-1,2:-2,3:-3,4:-4})
0   -1
1   -2
2   -3
3   -4
4    5
5    6
dtype: int64

关于replace替换的问题

11-07

程序一rnrndeclare @begin as nvarchar(1500),@end as nvarchar(1500),@a as nvarchar(150) ,@b as nvarchar(150)rndeclare @i as int,@count as int, @m as int,@j as int,@k as intrnset @i=(select min(a.id) from ( select Begindate,Enddate,id rnfrom TBL_Hk_co_perprofile rnwhere len(Begindate)>10 and len(enddate)>10) a )--从查询结果中查rnrn select @count=(select max(a.id) from ( select Begindate,Enddate,id from TBL_Hk_co_perprofile where len(Begindate)>10 and len(enddate)>10) a )rnwhile @i<=@countrnbeginrn [color=#FF0000]select @begin=replace((select begindate from( select Begindate,Enddate,id from TBL_Hk_co_perprofile where len(Begindate)>10 and len(enddate)>10) a where id =@i)+',','N/A','0000-00-00')rn select @end=replace(( select Enddate from ( select Begindate,Enddate,id from TBL_Hk_co_perprofile where len(Begindate)>10 and len(enddate)>10) a where [ID]=@i )+',','N/A','9999-00-00')rn[/color] rn set @m =LEN(@begin)rn rn while @m>0rn beginrn set @j=patindex('%,%',@begin)rn set @k=patindex('%,%',@end)rn set @a=substring(@begin,1,@j-1)--截取下一个逗号前的字符rn set @b=substring(@end,1,@k-1)rn rn if @a>@b rn beginrn select id,begindate,Enddate from TBL_Hk_co_perprofile where id=@irn endrn select @begin=substring(@begin,patindex('%,%',@begin)+1,len(@begin)-patindex('%,%',@begin))rn select @end=substring(@end,patindex('%,%',@end)+1,len(@end)-patindex('%,%',@end))rn rn select @m=LEN(@begin)rn endrn rn rn select @i=(select a.idrn from ( select Begindate,Enddate,id from TBL_Hk_co_perprofile where len(Begindate)>10 and len(enddate)>10) a rn where a.id in (select top 1 a.idrn from ( select Begindate,Enddate,id from TBL_Hk_co_perprofile where len(Begindate)>10 and len(enddate)>10) a rn where id>@irn order by id ))--查询下一条idrnrnrnendrnrngornrnrn程序二rnrnrndeclare @begin as nvarchar(1500),@end as nvarchar(1500),@a as nvarchar(150) ,@b as nvarchar(150)rndeclare @i as int,@count as int, @m as int,@j as int,@k as intrnset @i=(select min(a.id) from ( select Begindate,Enddate,id rnfrom TBL_Hk_co_perprofile rnwhere len(Begindate)>10 and len(enddate)>10) a )--从查询结果中查rnrn select @count=(select max(a.id) from ( select Begindate,Enddate,id from TBL_Hk_co_perprofile where len(Begindate)>10 and len(enddate)>10) a )rnwhile @i<=@countrnbeginrn [color=#FF0000]select @begin=(select begindate from( select Begindate,Enddate,id from TBL_Hk_co_perprofile where len(Begindate)>10 and len(enddate)>10) a where id =@i)+','rn select @end=( select Enddate from ( select Begindate,Enddate,id from TBL_Hk_co_perprofile where len(Begindate)>10 and len(enddate)>10) a where [ID]=@i )+','[/color]rn rn set @m =LEN(@begin)rn rn while @m>0rn beginrn set @j=patindex('%,%',@begin)rn set @k=patindex('%,%',@end)rn set @a=substring(@begin,1,@j-1)--截取下一个逗号前的字符rn set @b=substring(@end,1,@k-1)rn rn if @a>@b rn beginrn select id,begindate,Enddate from TBL_Hk_co_perprofile where id=@irn endrn select @begin=substring(@begin,patindex('%,%',@begin)+1,len(@begin)-patindex('%,%',@begin))rn select @end=substring(@end,patindex('%,%',@end)+1,len(@end)-patindex('%,%',@end))rn rn select @m=LEN(@begin)rn endrn rn rn select @i=(select a.idrn from ( select Begindate,Enddate,id from TBL_Hk_co_perprofile where len(Begindate)>10 and len(enddate)>10) a rn where a.id in (select top 1 a.idrn from ( select Begindate,Enddate,id from TBL_Hk_co_perprofile where len(Begindate)>10 and len(enddate)>10) a rn where id>@irn order by id ))--查询下一条idrnrnrnendrnrngorn 红色部分,为什么程序一做替换后不出结果,程序二是正常运行的 但是有N/A,我想用相应的字符替换,求大神指导rnrn

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试