replace-替换2019/1/4

1.函数:

df.replace(to_replace=None, value=None, inplace=False,
limit=None, regex=False, method='pad') #value替换给定的值to_replace
# replace动态替换不同于.loc更新(需指定要更新的位置)参数:
to_replace:str,regex,list,dict,Series,int,float或None#将被替换的值1)numeric,str或regex:等于或匹配的值将被替换2)str,regex或numeric列表:# to_replace和value都是列表长度必须相同# regex=True两列表中字符串都被解释为正则表达式,否则将直接匹配。3)字典:# 直接替换:to_replace= {'a': 'b', 'y': 'z'},value=None# 指定列:{'A': 0, 'B': 5}, value # 用值替换A,B列中的数值# 嵌套的典:{'A': {'b':np.nan}}) # A列b被None替换,b可是RE,A不可以;value=None:标量,字典,列表,str,正则表达式;#要替换的值
inplace=False  #True就地修改
limit=None:int#向前或向后填充的数量regex=False:bool或与to_replace相同的类型;正则表达式# to_replace=r'^ba.$', value=str, regex=True# to_replace={'A': r'^ba.$'}, value={'A': str}, regex=True# regex=r'^ba.$', value=str #regex为字符串to_replace必为None# regex={r'^ba.$':str, 'foo':str}# regex=[r'^ba.$', str], value=strmethod='pad':{'pad','ffill','bfill',None}#用前面或后面的值替换

2.实例1:

# 实例1.1:to_replace=标量;value=标量
s = pd.Series([0, 1, 2, 3, 4])
df = pd.DataFrame({'A': [0,1,2,3,4], 'B': [5,6,7,8,9],'C': ['a','b','c','d','e']})s.replace(0, 5)  # 用5替换0
df.replace(0, 5) # 用5替换0# 实例1.2:to_replace=列表;value=标量或列表
df.replace([0, 1, 2, 3], 4)            # 用4替换0,1,2,3,4
df.replace([0, 1, 2, 3], [4, 3, 2, 1]) # 4,3,2,1分别替换0,1,2,3# 实例1.3:to_replace=dict;value=None或标量
df.replace({0: 10, 1: 100})         # 10替换0,100替换1
df.replace({'A': 0, 'B': 5}, 100)   # A列0,B列5都用100替换
df.replace({'A': {0: 100, 4: 400}}) # A列0被100替换,4被400替换s.replace([1, 2], method='bfill')   # 后项替换即用3替换1,2df = pd.DataFrame({'A': [1,2,3,4,5,6], 'B': [-1,10,11,12,12,14]})
df.replace({1: 10, 2000: 60, 30: -30, 40: -40})
# 实例2:正则表达式:  
df = pd.DataFrame({'A': ['bat', 'foo', 'bait'],'B': ['abc', 'bar', 'xyz']})result1=df.replace(to_replace=r'^ba.$', value='new', regex=True)#结果同下
result1=df.replace(regex=r'^ba.$', value='new')                 #等价
result2=df.replace({'A': r'^ba.$'}, {'A': 'new'}, regex=True)
result3=df.replace(regex={r'^ba.$':'new', 'foo':'xyz'})
result4=df.replace(regex=[r'^ba.$', 'foo'], value='new')# result1 result2 result3 result4A   B        A B        A B        A B
0 new abc  0 new abc  0 new abc  0 new abc
1 foo new  1 foo bar  1 xyz new  1 new new
2 bait xyz 2 bait xyz 2 bait xyz 2 bait xyz
# 实例3:替换bool或datetime64数据类型必须匹配 
df = pd.DataFrame({'A': [True, False, True],'B': [False, True, False]})
df.replace({'a string': 'new value', True: False}) # 错误数据类型不匹配
#实例4 method 
s = pd.Series([0, 1, 2, 3, 4])
result1=s.replace([2,3],method='pad')
result1=s.replace([2,3],method='ffill')
result2=s.replace([2,3],method='bfill')# s      result1   result2
0 0    0 0       0 0
1 1    1 1       1 1
2 2    2 1       2 4
3 3    3 1       3 4
4 4    4 4       4 4
dtype: int64 dtype: int64 dtype: int64
#实例5 
s = pd.Series([1, 'a', 'a', 'b', 'a'])result1=s.replace('a', None)
result1=s.replace(to_replace='a', value=None, method='pad') #等价
result2=s.replace({'a': None})
result2=s.replace(to_replace={'a': None}, value=None, method=None)#等价# s             result1       result2
0 1           0 1           0 1
1 a           1 1           1 None
2 a           2 1           2 None
3 b           3 b           3 b
4 a           4 b           4 None
dtype: object dtype: object dtype: object

pandas39 replace替换数值( tcy)相关推荐

  1. R语言replace函数数值替换实战

    R语言replace函数数值替换实战 目录 R语言replace函数数值替换实战 #基本语法 #仿真数据 #replace函数应用

  2. js正则表达式/replace替换变量方法

    转自:http://www.blogjava.net/pingpang/archive/2012/08/12/385342.html 1. javascript 正则对象替换创建和用法:/patter ...

  3. SQL查询中用replace替换ntext,text字段的各种方法总结

    方法一(推荐): update tablename set fieldA=replace(cast(fieldA as varchar(8000)) ,'aa','bb')这样的语句. SQL中rep ...

  4. js 正则表达式奇偶字符串替换_js正则表达式replace替换变量方法

    JavaScript正则实战(会根据最近写的不断更新) 1.javascript 正则对象替换创建 和用法: /pattern/flags 先简单案例学习认识下replace能干什么 正则表达式构造函 ...

  5. Pandas中replace替换问题

    replace参数:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.replace.html 最 ...

  6. MySQL中 replace 替换字符

    阅读目录 语法:replace(field,search,replace) 说明: field - 数据库表的列名 search - 需要替换的字符串 replace - 替换成的字符串 语义:将列名 ...

  7. JS replace替换字符串所有匹配字符

    之前竟然一直没发现,以为前端的replace和后端的replace方法作用相同,都是替换字符串中所有指定字符,其实不然的. replace 替换一个 let str = "2021-03-1 ...

  8. Js中replace替换全部

    Js中replace替换所有* var t = '***感**谢**有**你***'; var r = t.replace(/\*/g,''); //\为转义字符 g表示全局 console.log( ...

  9. java替换换行符_Java的replace替换字符串中的回车换行符的方法

    Java的replace替换字符串中的回车换行符的方法 导语:Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承.指针等概念,因此Java语言具有功能强大 ...

最新文章

  1. Django mysql 多线程_【实例:利用Django管理后台管理IP地址】(四)Django test+多线程+数据库+(踩坑)...
  2. _disable_logging 对于归档数据库的影响
  3. Google Maps API 初级1
  4. 在EXCEL指定SHEET页,指定文字位置,插入批注
  5. BUUCTF--练习场-- basic--上传文件漏洞经典靶场upload-labs-- Pass1-3(Pass4简单尝试)
  6. 通过Flume简单实现Kafka与Hive对接(Json格式)
  7. Python可以这样学(第四季:数据分析与科学计算可视化)-董付国-专题视频课程...
  8. plotting and saving over line in paraView
  9. Linux中bond的七种网卡绑定模式详解
  10. 1.1 电 电流 电压 电路 基本电子元件
  11. Application.platform 平台
  12. 边缘计算(一)——边缘计算的兴起
  13. 在贴吧怎样引流宝妈粉?怎么从百度贴吧引流宝妈粉?
  14. upperbound找不到_lower_bound()函数和upper_bound()函数,以及二分查找
  15. 2021高考本溪高中成绩查询,本溪市高级中学2020—2021学年度(上学期) 高一高二期中表奖大会...
  16. linux中pwd和oldpwd区别,pwd和cd命令详解
  17. 小白入智能小车坑(一)
  18. 学以致用--游戏:孢子(Spore) 中 殖民地 最佳布局
  19. tomcat宕机无法响应问题研究解决
  20. 施耐德电气为福建申远聚酰胺一体化项目提供智能配电方案

热门文章

  1. Java解析rdf和xml文件以及处理excel
  2. App自动化元素定位技巧__根据 XPATH 定位
  3. 细说Java学习的捷径与方法
  4. manjaro无法上网_Manjaro安装,配置,美化指南(可能是全网最全)
  5. 解决zeal启动闪退(启动时关闭代理)
  6. Spring、Spring MVC、Spring boot、Spring Cloud面试题(史上最全面试题,精心整理100家互联网企业,面试必过)
  7. unbuntu22.04安装QQ音乐并解决无法打开问题
  8. 阿里云服务器CPU处理器性能测评
  9. 图片转文字软件有哪些?办公常备软件
  10. Java实现桌面鼠标坐标获取