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 ...
随机推荐
- JDBC(12)—DBUtils工具类
DBUtils:commons-dbutils是Apache组织提供的一个开源JDBC工具库,它是对JDBC的简单封装,并且使用dbutils会极大的简化jdbc编码的工作量,同时不会影响到程序的性能 ...
- Codeforces899C Dividing the numbers(数论)
http://codeforces.com/problemset/problem/899/C tot为奇数时,绝对差为1:tot为偶数时,绝对差为0. 难点在于如何输出. #include<io ...
- #define LT(a,b) ((a)<(b))
就是带参数的宏定义 LT是函数名 (a,b )是参数表((a)<(b))是表达式返回一个布尔类型的值
- 开源流媒体服务器SRS学习笔记(4) - Cluster集群方案
单台服务器做直播,总归有单点风险,利用SRS的Forward机制 + Edge Server设计,可以很容易搭建一个大规模的高可用集群,示意图如下 源站服务器集群:origin server clus ...
- Jetpack 架构组件 Paging 分页加载 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- postgresql中使用distinct去重
select语法 [ WITH [ RECURSIVE ] with_query [, ...] ] SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ...
- WebApp开发技术搭配
一:Ionic + angular + cordova + zipalign UI框架:lonic+angular 开发与编译打包框架:Cordova 优化工具:zipalign 二:MUI+HBui ...
- Oracle JDBC驱动安装到Maven本地仓库
Oracle JDBC驱动因为授权问题,没有放到Maven的中央仓库里面,当然了,阿里云的镜像也没有了.所以要从Oracle官网下载驱动: 注意下载ojdbc6.jar 因为这个JDK1.8才能用. ...
- webstorm激活方法webstorm注册码 jetbrains激活
安装完成后,打开 WebStorm, 在打开的 License Activation 窗口中选择 License server. 在输入框输入网址即可: http://idea.codebeta.cn ...
- Django表单介绍
HTML 表单 在HTML中,表单是<form>...</form> 之间元素的集合,它们允许访问者输入文本.选择选项.操作对象和控制等等,然后将信息发送回服务器. 某些表单的 ...