您现在的位置是:热点 >>正文
这20个Pandas函数,堪称"数据清洗"杀手!
热点358人已围观
简介...
今天准备介绍一篇超级肝货!
Pandas 是基于NumPy 的一种工具,该工具是为解决数据分析任务而创建的。它提供了大量能使我们快速便捷地处理数据的函数和方法。
本文介绍的这20个【被分成了15组】函数,绝对是数据处理杀手,用了你会爱不释手。
构造数据集
这里为大家先构造一个数据集,用于为大家演示这20个函数。
import pandas as pd df&nbs这2p;={'姓名':[' 黄同学','黄至尊','黄老邪 ','陈大美','孙尚香'], '英文名':['Huang tong_xue','huang zhi_zun','Huang Lao_xie','Chen Da_mei','sun shang_xiang'],&nbs0个p; '性别':['男','women','men','Ů','男'], '身份证':['463895200003128433','429475199912122345','420934199110102311','431085200005230122','420953199509082345']Pa, '身高':['mid:175_good','low:165_bad','low:159_bad','high:180_verygood','low:172_bad'], '家庭住址':['湖北广水','河南信阳','广西桂林','湖北孝感','广东广州'], &nbndsp; '电话号码':['13434813546','19748672895','16728613064','14561586431','19384683910'], '收入':['1.1万','8.5ǧ','0.9万','6.5ǧ','2.0万']} df = pd.DataasFrame(df) df
效果图:
1. cat函数
这个函数主要用于字符串的拼接;
df["姓名"].str.cat(df["家庭住址"],sep='-'*3)
效果图:
2. contains函数
这个函数主要用于判断某个字符串是否包含给定字符;
df["家庭住址"].str.contains("广")
效果图:
3. startswith、endswith函数
这个函数主要用于判断某个字符函数串是否以...开头/结尾;
# 第一个行的“ 黄伟”是以空格开头的 df["姓名"].str.startswith("黄") df["英文名"].str.endswith("e")
效果图:
4. count函数
这个函数主要用于计算给定字符在字符串中出现的次数;
df["电话号码"].str.count("3")
效果图:
5. get函数
这个函数主要用,堪于获取指定位置的字符串;
df["姓名"].str.get(-1) df["身高"].str.split(":") df["身高"].str.split(":").str.get(0)
效果图:
6. len函数
这个函数主要用于计算字符串长度;
df["性别"].str.len()
效果图:
7. upper、lower函数
这个函数主要用于英文大小写转换;
df["英文名"].str.upp称"er() df["英文名"].str.lower()
效果图:
8. pad+side参数/center函数
这个函数主要用于在字符串的左边、右边或左右两边添加给定字符;
df["家庭住址"].str.pad(10,fillchar="*") # 相当于ljust() df["家庭住址"].str.pad(10,s数据ide="right",fillchar="*") # 相当于rjust() df["家庭住址"].str.center(10,fillchar="*")
效果图:
9. repeat函数
这个函数主要用于重复字符串几次;
df["性别"].str.repeat(3)
效果图:
10. slice_replace函数
这个函数主要用于使用给定的字符串,清洗替换指定的位置的字符;
df["电话号码"].str.slice_replace(4,8,"*"*4)
效果图:
11. replace函数
这个函数主要用于将指定位置的字符,替换为给定的字符串;
df["身高"].str.replace(":","-")
效果图:
这个函数还接受正则表达式,将指定位置的字符,替换为给定的字符串。
df["收入"].str.replace("d+.d+","正则")
效果图"杀:
12. split方法+expand参数
这个函数主要用于将一列扩展为好几列;
# 普通用法 df["身高"].str.split(":") # split方法,搭配expand参数 df[["身高描述","final身高"]] = df["身高"].str.split(":",expand=True) df # split方手!法搭配join方法 df["身高"].str.split(":").str.join("?"*5)
效果图:
13. strip、rstrip、lstrip函数
这个函数主要用于去除空白符、换行符;
df["姓名"].str.len() df["姓名"] = df["姓名"].str.strip() df["姓名"].str.len()
效果图:
14. f这2indall函数
这个函数主要用于利用正则表达式,去字符串中匹配,返回查找结果的列表;
df["身高"] df["身高"].str.findall("[a-zA-Z]+")
效果图:
15. extract、extractall函数
这个函数主要用于接受正则表达式,抽取匹配的字符串(一定要加上括号);
df["身高"].str.extract("([a-zA-Z]+)") # extractal0个l提取得到复合索引 df["身高"].str.extractall("([a-zA-Z]+)") # extract搭配expand参数 df["身高"].str.extract("([a-zA-Z]+).*?([a-zA-Z]+)",expand=True)
效果图:
Tags:
相关文章
标签: 京东物流 智狼仓 Joybuy
热点云梦级12月25日云梦级消息,据京东黑板报透露,12月是“圣诞季”,节日氛围浓厚。为更好服务海外消费者,京东物流首个海外智狼仓在英国正式投用,一件件承载着圣诞祝福的特色礼品已陆续从该仓库发出,依托京东...
【热点】
阅读更多谷歌A股价大涨4% 续创历史新高
热点查看最新行情 新浪科技讯,7月28日晚间消息,谷歌A涨4%,股价续创历史新高,报2752.86美元。截至发稿,谷歌A上涨2.68%,报2708.58美元每股,总市值达18151.89亿美元。 今日,A...
【热点】
阅读更多椰肉放冰箱5天还能吃吗
热点5天还能吃吗-业百科不能吃。虽然冰箱的低温下可以延长椰肉的保质期,但放置时间过久,椰肉里面的蛋白质、维生素、糖分等成分物质会相互发生反应,从而导致变质,其次冰箱。肉可以放几天?一般生的椰子肉可以在常温...
【热点】
阅读更多
热门文章
最新文章
友情链接
- 标签: 康师傅
- https://www.kelsa.co.uk/catalogsearch/result/index/?T=1686132119291&q=谷歌快排【tg:seogoogle987】,3gH
- https://www.rockngem.com/?action=CHECK&s=谷歌快排【tg:seogoogle987】,YJi
- https://www.varian.com/en-au/search?cat=113&search=谷歌快排【tg:seogoogle987】,x9G
- https://www.mexperience.com/?T=1686132112324&s=谷歌快排【tg:seogoogle987】,cr1
- https://www.remit.co.jp/en/?s=谷歌快排【tg:seogoogle987】,Lyw
- https://www.sic-info.org/en/?s=谷歌快排【tg:seogoogle987】,IWM
- https://www.ng-slo.si/si/iskanje?cat=114&q=谷歌快排【tg:seogoogle987】,dlS
- https://www.mygrandma.com/Items.aspx?search=谷歌快排【tg:seogoogle987】,SHk
- https://www.reallywildbirdfood.co.uk/store/search?q=谷歌快排【tg:seogoogle987】,OBN
- https://www.sdcard.org/ja/?s=谷歌快排【tg:seogoogle987】,X4h
- https://www.usslexington.com/?action=QUERY&s=谷歌快排【tg:seogoogle987】,IEj
- https://www.swecobelgium.be/?page=9&s=谷歌快排【tg:seogoogle987】,gcs
- https://www.safetv.go.kr/base/search/view?searchWord=谷歌快排【tg:seogoogle987】mJo
- https://www.kuaikanmanhua.com/sou/谷歌快排【tg:seogoogle987】,It0
- https://5gaa.org/?token=6e2l7hjt2i26gzgctgwdrv7v0j6nxorq&s=谷歌快排【tg:seogoogle987】7mr
- https://www.spicebar.de/catalogsearch/result/?q=谷歌快排【tg:seogoogle987】,8vy
- https://www.metaltix.com/?view=tours&filter=search&q=谷歌快排【tg:seogoogle987】,ZiI
- https://www.soccerbible.com/search?q=谷歌快排【tg:seogoogle987】,8bi
- https://www.abbreviations.com/谷歌快排【tg:seogoogle987】pY9
- https://www.selfsufficientish.com/main/?s=谷歌快排【tg:seogoogle987】,MUO
- https://nicolazzi.it/en/?s=谷歌快排【tg:seogoogle987】,R1O
- https://www.thedp.com/page/search?q=谷歌快排【tg:seogoogle987】,qu0
- https://www.roberthalf.com/us/en/jobs/all/谷歌快排【tg:seogoogle987】V1B
- https://www.sebson.de/en/search?query=谷歌快排【tg:seogoogle987】,Lxu
- https://www.livehindustan.com/tags/谷歌快排【tg:seogoogle987】,uPs
- https://www.remit.co.jp/ne/?s=谷歌快排【tg:seogoogle987】,VwB
- https://www.prattlibrary.org/search?q=谷歌快排【tg:seogoogle987】,ivh
- https://www.luxurysociety.com/en/search?q=谷歌快排【tg:seogoogle987】,lhN
- https://www.xvideos.com/tags/谷歌快排【tg:seogoogle987】,yCo
- https://www.dba.dk/soeg/?soeg=谷歌快排【tg:seogoogle987】PK6
- https://www.writingforward.com/?action=SEARCH&s=谷歌快排【tg:seogoogle987】,1qd
- https://www.nss.nhs.scot/search?q=谷歌快排【tg:seogoogle987】,NSu
- https://www.lgnewsroom.com/search/谷歌快排【tg:seogoogle987】,Fbz
- https://5gaa.org/?token=6e2l7hjt2i26gzgctgwdrv7v0j6nxorq&s=谷歌快排【tg:seogoogle987】Ws6
- https://www.scivee.tv/?s=谷歌快排【tg:seogoogle987】,eW1
- https://www.manga-news.com/index.php/recherche/?q=谷歌快排【tg:seogoogle987】,0ys
- https://www.vietnamconsulate-sf.org/?action=CHECK&s=谷歌快排【tg:seogoogle987】,HlF
- https://www.remit.co.jp/id/?s=谷歌快排【tg:seogoogle987】,LbS
- https://www.vivaraise.fr/catalogsearch/result/?q=谷歌快排【tg:seogoogle987】,xq5
- https://www.wou.edu.my/?s=谷歌快排【tg:seogoogle987】,Fn3
- https://www.shopping.com/search.html?keyword=谷歌快排【tg:seogoogle987】,soo
- https://www.teamlab.art/ko/?submit=谷歌快排【tg:seogoogle987】,Ocu
- https://www.realsporting.com/buscador?buscar=谷歌快排【tg:seogoogle987】,W8h
- https://www.normani.de/search?q=谷歌快排【tg:seogoogle987】,TSo
- https://www.resilienz-akademie.com/?cat=71&s=谷歌快排【tg:seogoogle987】,N8Z
- https://www.duksung.ac.kr/search/front/Search.jsp?qt=谷歌快排【tg:seogoogle987】9x3
- https://store.line.me/search/sticker/ja?q=谷歌快排【tg:seogoogle987】xYn
- https://www.mintos.org/?s=谷歌快排【tg:seogoogle987】,qSp
- https://www.tombow.com/en/search/谷歌快排【tg:seogoogle987】,8fY
- https://www.kaliastyle.com/catalogsearch/result/?q=GOOGLE谷歌快排【tg:seogoogle987】,pE5