pandas replace 替换功能function
import pandas as pd
import numpy as np
s = pd.Series([0,1,2,3,4])
s.replace(0,5) # single value to replace
0 5
1 1
2 2
3 3
4 4
dtype: int64
df = pd.DataFrame({'A':[0,1,2,3,4],
"B":[5,6,7,8,9],
"C":['a','b','c','d','e']})
df.replace(0,5) # replace all 0 to 5
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| A | B | C | |
|---|---|---|---|
| 0 | 5 | 5 | a |
| 1 | 1 | 6 | b |
| 2 | 2 | 7 | c |
| 3 | 3 | 8 | d |
| 4 | 4 | 9 | e |
df # the default parameter in_place= False
# DataFrame.replace(to_replace=None, value=None, inplace=False, limit=None, regex=False, method='pad')
# to_place can be number,string list or dict and even regex expression
# limit Maximum size gap to forward or backward fill.
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| A | B | C | |
|---|---|---|---|
| 0 | 0 | 5 | a |
| 1 | 1 | 6 | b |
| 2 | 2 | 7 | c |
| 3 | 3 | 8 | d |
| 4 | 4 | 9 | e |
1. list like replace method
df.replace([1,2,3,4],[4,3,2,1]) # content to replace . to_replace=[1,2,3,4],value=[4,3,2,1]
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| A | B | C | |
|---|---|---|---|
| 0 | 0 | 5 | a |
| 1 | 4 | 6 | b |
| 2 | 3 | 7 | c |
| 3 | 2 | 8 | d |
| 4 | 1 | 9 | e |
df.replace([1,2,3,4],100) # to_replace=[1,2,3,4],value=4
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| A | B | C | |
|---|---|---|---|
| 0 | 0 | 5 | a |
| 1 | 100 | 6 | b |
| 2 | 100 | 7 | c |
| 3 | 100 | 8 | d |
| 4 | 100 | 9 | e |
df.replace([1,2],method='bfill') # . like fillna with mehtod bfill(backfill), and the default mehtod was pad
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| A | B | C | |
|---|---|---|---|
| 0 | 0 | 5 | a |
| 1 | 3 | 6 | b |
| 2 | 3 | 7 | c |
| 3 | 3 | 8 | d |
| 4 | 4 | 9 | e |
2. dict like replace method
df.replace({2:20,6:100}) # to_replace =2 value=20,to_replace=6,value =100
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| A | B | C | |
|---|---|---|---|
| 0 | 0 | 5 | a |
| 1 | 1 | 100 | b |
| 2 | 20 | 7 | c |
| 3 | 3 | 8 | d |
| 4 | 4 | 9 | e |
df.replace({'A':2,'B':7},1000) # . to_replace={'A':2,"B":7}, value=1000
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| A | B | C | |
|---|---|---|---|
| 0 | 0 | 5 | a |
| 1 | 1 | 6 | b |
| 2 | 1000 | 1000 | c |
| 3 | 3 | 8 | d |
| 4 | 4 | 9 | e |
df.replace({'A':{1:1000,4:20}}) # in colomn A to_replace=1,value=1000, to_replace=4, value=20
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| A | B | C | |
|---|---|---|---|
| 0 | 0 | 5 | a |
| 1 | 1000 | 6 | b |
| 2 | 2 | 7 | c |
| 3 | 3 | 8 | d |
| 4 | 20 | 9 | e |
3. regex expression
df = pd.DataFrame({'A':['bat','foot','bait'],
'B':['abc','bar','foot']})
df.replace(to_replace=r'^ba.$',value='vvvv',regex=True) # to define to_replace and value in the function
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| A | B | |
|---|---|---|
| 0 | vvvv | abc |
| 1 | foot | vvvv |
| 2 | bait | foot |
df.replace({'A': r'^ba.$'}, {'A': 'new'}, regex=True) # in column A to_replce=r'^ba.$' value='new'
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| A | B | |
|---|---|---|
| 0 | new | abc |
| 1 | foot | bar |
| 2 | bait | foot |
df.replace({'A':{r"^ba.$":"new"}},regex=True) # same as above
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| A | B | |
|---|---|---|
| 0 | new | abc |
| 1 | foot | bar |
| 2 | bait | foot |
df.replace(regex=r'^ba.$',value='vvv') # in the whole dataframe
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| A | B | |
|---|---|---|
| 0 | vvv | abc |
| 1 | foot | vvv |
| 2 | bait | foot |
df.replace(regex={r'^ba.$':'vvv','foot':'xyz'})
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| A | B | |
|---|---|---|
| 0 | vvv | abc |
| 1 | xyz | vvv |
| 2 | bait | xyz |
df.replace(regex=[r'^ba.$','foo.$'],value='vvv')
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| A | B | |
|---|---|---|
| 0 | vvv | abc |
| 1 | vvv | vvv |
| 2 | bait | vvv |
pandas replace 替换功能function的更多相关文章
- Python3.5 day3作业一:实现简单的shell sed替换功能
需求: 1.使python具有shell中sed替换功能. #!/usr/bin/env python #_*_conding:utf-8_*_ #sys模块用于传递参数,os模块用于与系统交互. i ...
- 关于js的replace替换
关于js的replace替换 msgContent = msgContent.replace("a","b"); 这样的替换只会把第一个a替换成b,不会替换全部 ...
- Java基础知识强化40:StringBuffer类之StringBuffer的替换功能
1. StringBuffer的替换功能: public StringBuffer replace(int start, int end, String str): 2. 案例演示: p ...
- js replace替换字符串,同时替换多个方法
在实际开发中,经常会遇到替换字符串的情况,但是大多数情况都是用replace替换一种字符串,本文介绍了如何使用replace替换多种指定的字符串,同时支持可拓展增加字符串关键字. let conten ...
- 在go modules中使用replace替换无法直接获取的package(golang.org/x/...)
上一篇里我们介绍了使用go get进行包管理. 不过因为某些未知原因,并不是所有的包都能直接用go get获取到,这时我们就需要使用go modules的replace功能了.(当然大部分问题挂个梯子 ...
- [转载]js正则表达式/replace替换变量方法
原文地址:http://www.blogjava.net/pingpang/archive/2012/08/12/385342.html JavaScript正则实战(会根据最近写的不断更新) 1.j ...
- Python3学习之路~2.8 文件操作实现简单的shell sed替换功能
程序:实现简单的shell sed替换功能 #实现简单的shell sed替换功能,保存为file_sed.py #打开命令行输入python file_sed.py 我 Alex,回车后会把文件中的 ...
- EmEditor的一个好用的正则替换功能
最近在编辑文本的时候用到了EmEditor的一个好用的正则替换功能.即我想用搜索到内容的一部分来生成另一段文本.例如客户提供给我一大堆MYSQL的建立主键的脚本,我想改成MSSQL的建立主键的脚本,这 ...
- 3-1 实现简单的shell sed替换功能
1.需求 程序1: 实现简单的shell sed替换功能 file1 的内容copy到file2 输入参数./sed.py $1 $2 $1替换成$2 (把a替换成% ) 2.个人思路 open ...
随机推荐
- DBS:TestSystem
ylbtech-DBS:TestSystem A, 返回顶部 2. -- ================================= -- 类别表 -- ================= ...
- 微信小程序登录逻辑
wx.getStorage({ key: 'session_id', success: function(res) { //如果本地缓存中有session_id,则说明用户登陆过 console.lo ...
- iOS 渐变提示。错误弹出提示 几秒自动消失
//事例 CGRect alertFarm = CGRectMake(,,,); [self noticeAlert:_bgView withNoticeStr:@"登录成功" w ...
- Exception thrown on Scheduler.Worker thread. Add `onError` handling
<html> <head></head> <body> java.lang.IllegalStateException: Exception throw ...
- 读取PBOC电子现金指令流
该指令流仅适用于T=0协议卡片. 终端对IC卡的响应: 60 须要额外的工作等待时间,说明IC卡端数据还未处理好. 61 发送GET RESPONSE命令取应答数据 6C 加上取字节数,命令重发 ...
- shell脚本启动语法错误syntax error near unexpected token '{
执行shell脚本时失败,报语法错误,但脚本内容检查正常 原因为该脚本是在非Linux系统下编辑之后放到系统执行的,文件模式类型非Linux系统匹配的模式类型. 查看文件的模式类型 显示文件的模式类型 ...
- Docker在windows下的使用【一】
1.windows按照docker的基本要求 (1)64为操作系统,win7或者更高 (2)支持“ Hardware Virtualization Technology”,并且,“virtualiza ...
- ios 容错处理JKDataHelper和AvoidCrash
一.JKDataHelper 在大团队协同开发过程中,由于每个团队成员的水平不一,很难控制代码的质量,保证代码的健壮性,经常会发生由于后台返回异常数据造成app崩溃闪退的情况,为了避免这样情况使用JK ...
- 转 : 深入解析Java锁机制
深入解析Java锁机制 https://mp.weixin.qq.com/s?__biz=MzU0OTE4MzYzMw%3D%3D&mid=2247485524&idx=1&s ...
- Python时间,日期,时间戳之间转换,时间转换时间戳,Python时间戳转换时间,Python时间转换时间戳
#1.将字符串的时间转换为时间戳方法: a = "2013-10-10 23:40:00" #将其转换为时间数组 import time timeArray = time.strp ...