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 ...
随机推荐
- How to do conditional auto-wiring in Spring?
ou can implement simple factory bean to do the conditional wiring. Such factory bean can contain com ...
- /etc/passwd /etc/group /etc/shadow 文件的格式说明
/etc/passwd 存放账户信息: root:x:0:0:root:/root:/bin/bashjianing:x:1011:100::/home/jianing:/bin/bashuserna ...
- Java利用ShutDownHook关闭系统资源
Java关闭钩子 在Java程序中能够通过加入关闭钩子,实如今程序退出时关闭资源的功能. 使用Runtime.addShutdownHook(Thread hook)向JVM加入关闭钩子 public ...
- 创建MySQL用户 赋予某指定库表的权限
摘自: http://renxiangzyq.iteye.com/blog/763837 update ERROR 1364 (HY000): Field 'ssl_cipher' doesn't h ...
- centos exfat格式U盘不支持问题
centos exfat格式U盘不支持问题 1. 下载fuse-exfat-1.3.0-1.el7.x86_64.rpm 2. 终端安装 rpm -ivh fuse-exfat-1.3.0-1.el ...
- Navicat for MySQL 64位破解版
注册码:NAVH-WK6A-DMVK-DKW3 百度网盘链接(为安装包,直接运行EXE文件即可):http://pan.baidu.com/s/1o7OwjFG
- Lombok 使用攻略
1. Lombok 简介 Lombok 可以通过简单的注解来帮助我们简化消除一些必须有但显得很臃肿的Java代码,通过使用对应的注解,可以在编译源码的时候生成对应的方法. Lombok 既是一个 ID ...
- numpy中的方差、协方差、相关系数
一.np.var 数学上学过方差:$$ D(X)=\sum_{i\in [0,n)} ({x-\bar{x}})^2 $$ np.var()实际上是均方差,均方差的意义就是将方差进行了平均化,从而使得 ...
- Linux内核剖析(五)Linux内核的构建过程
参考 一次实验引发的故事 – kernel build system探索—vmlinux是如何炼成的– kernel makefile 深度探索Linux操作系统:系统构建和原理解析.pdf 问题 在 ...
- 微表面分布函数(Microfacet Distribution Function)确切含义
<Physically-Based Shading Models in Film and Game Production>中说:“D()的值不局限于0到1,可以任意大”,这句话使我比较好奇 ...