pandas实现手机号号码中间4位匿名化的示例代码
作者:python小工具 时间:2022-02-02 06:08:40
因为工作上要将客户的部分资料传给第三方做进一步处理,但是因为涉及到手机号等关键个人信息,所以需要对中间四位数做匿名化的简单处理。
>>> import pandas as pd
>>> import numpy as np
# 这里需要将手机号所在列在读取时指定类型
>>> df = pd.read_excel('D:/1.xlsx', dtype={"phone":str})
>>> df
phone
0 18373827382
1 18373824382
2 18373827382
3 18373427382
4 18373857382
5 18373827382
6 18373427382
7 18373817382
# 代码也很简单,只是分别取手机号的
# 前三位和后四位并添加*号拼接
>>> df['phone_replace']=df['phone'].str[0:3] + "****" + df['phone'].str[7:]
>>> df
phone phone_replace
0 18373827382 183****7382
1 18373824382 183****4382
2 18373827382 183****7382
3 18373427382 183****7382
4 18373857382 183****7382
5 18373827382 183****7382
6 18373427382 183****7382
7 18373817382 183****7382
正则隐藏手机号中间四位,改为**** (常见四种方法)
var phone='13511115678';
//方法一
var newphone=phone.substr(0, 3) + '****' + phone.substr(phone.length - 4);
//方法二
var newphone= phone.replace(phone.substr(3, 4),"****")
//方法三
var newphone=phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$3');
//方法四
var newphone=phone.replace( /([0-9]{3})([0-9]{4})([0-9]{4})/,"$1****$3")
来源:https://blog.csdn.net/weixin_45144170/article/details/114262030
标签:pandas,手机号,匿名
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Sanic框架异常处理与中间件操作实例分析
2023-04-30 00:27:31
python爬虫实战项目之爬取pixiv图片
2021-04-17 00:01:29
![](https://img.aspxhome.com/file/2023/8/125188_0s.jpg)
Python线程之定位与销毁的实现
2023-09-25 11:47:19
![](https://img.aspxhome.com/file/2023/1/90851_0s.png)
Python管理Windows服务小脚本
2023-11-07 17:30:59
Python破解excel进入密码的过程详解
2021-06-14 03:06:25
![](https://img.aspxhome.com/file/2023/7/89337_0s.png)
pandas or sql计算前后两行数据间的增值方法
2022-08-08 09:36:30
PyCharm如何配置SSH和SFTP连接远程服务器
2022-04-27 01:57:28
![](https://img.aspxhome.com/file/2023/8/131298_0s.png)
利用Python操作excel表格的完美指南
2022-05-21 07:59:09
![](https://img.aspxhome.com/file/2023/2/120972_0s.png)
详细聊聊为什么Python中0.2+0.1不等于0.3
2021-08-19 12:12:35
![](https://img.aspxhome.com/file/2023/7/86497_0s.png)
Python面向对象实现方法总结
2022-03-11 08:50:41
PyCharm使用最多也最常用默认快捷键分享
2023-05-23 20:54:55
![](https://img.aspxhome.com/file/2023/9/81579_0s.png)
python实现npy格式文件转换为txt文件操作
2021-04-03 08:07:05
![](https://img.aspxhome.com/file/2023/5/92075_0s.jpg)
python密码学RSA密码解密教程
2023-07-29 17:01:47
python进程管理工具supervisor的安装与使用教程
2023-10-15 20:41:57
Python取出字典中的值的实现
2022-01-09 20:16:07
ChatGPT教你用Python实现BinarySearchTree详解
2023-11-18 15:26:18
![](https://img.aspxhome.com/file/2023/5/97585_0s.png)
Python网络编程之ZeroMQ知识总结
2022-07-06 20:16:25
![](https://img.aspxhome.com/file/2023/6/115006_0s.png)
python 使用递归回溯完美解决八皇后的问题
2023-03-07 21:48:22
![](https://img.aspxhome.com/file/2023/5/107635_0s.jpg)
python 同时读取多个文件的例子
2022-06-27 03:56:50
零基础学习python偏函数语法的推导方法步骤
2021-07-17 20:59:18