您现在的位置是:热点 >>正文
数据预处理的10个小技能,附Pandas实现
热点2141617人已围观
简介...
数据预处理常用的处理步骤,包括找出异常值、处理缺失值、过滤不合适值、去掉重复行、分箱、分组、排名、category转数值等,下面使用 pandas 解决这些最常见的预处理任务。
找出异常值常用两种方法:
标准差法:异常值平均值上下1.96个标准差区间以外的值 分位数法:小于 1/4分位数减去 1/4和3/4分位数差的1.5倍,大于3/4减去 1/4和3/4分位数差的1.5倍,都为异常值
技能1 :标准差法
import pandas as pd df = pd.DataFrame({'a':[1,3,np.nan],'b':[4,np.nan,np.nan]}) # 异常值平均值上下1.96个标准差区间以外的值 mea数据ngrade = df['a'].mean() stdgrade = df['a'].std() toprange = meangrade + stdgrade * 1.96 botrange = meangrade - stdgrade * 1.96 # 过滤区间外的值 copydf = df copydfcopydf = copydf.drop(copydf[copydf['a'] &预处nbsp; > toprange].index) copydfcopydf = copydf.drop(copydf[copydf['a'] < botrange].index) copydf
技能2:分位数法
q1 = df['a'].quantile(.25) q3 = df['a'].quantile(.75) iqr = q3-q1 toprange = q3 + iqr * 理的;1.5 botrange = q1 - iqr * 1.5 copydf = df copydfcopydf = copydf.drop(copydf[copydf['a'] > toprange].index) copydfcopydf = copydf.drop(copydf[copydf['a'] < botrange10].index) copydf
技能3:处理空值
np.nan 是 pandas 中常见空值,使用 dropna 过滤空值,axis 0 表示按照行,1 表示按列,how 默认为 any ,意思是只要有一个 nan 就过滤某行或某列,all 所有都为 nan
# axis 0 表示按照行,all 此行所有值都为 nan df.dropna(axis=0, how='all')
技能4:充填空值
空值一般使用某个统计值填充,如平均数、众数、中位数等,使用函数 fillna:
# 使用a列平均数填充列的空值,inplace true表示就地填充 df["a"].fillna(df["a"].mean(), inplace=True)
技个小能5:修复不合适值
假如某门课最高分100,如果出现 -2, 120 这样的值,显然不合理,使用布尔类型的Series对象修改数值:
df.loc[(df['a'] < -2,'a')] = 0 df.loc[(df['a'] >= 100,'a')] = 100
技能6:过滤重复值
过滤某列重复值,使用 drop_duplicated 方法,第一个参数为列名,keep关键字等于last:最后一次出现此值行:
df.drop_duplicates(['Names'], keep='last')
技能7:apply 元素级:去掉特殊字符
某列单元格含有特殊字符,如标点符号,使用元素级操作方法 apply 干掉它们:
import 技能string exclude = set(string.punctuation) def remove_punctuation(x): x = ''.join(ch for ch in x if ch not in exclude) return x # ԭdf Out[26]: a ,附 b 0 c,d edc.rc 1 3 3 2 d ef 4 # 过滤a列标点 In [27]: dfdf.a = df.a.apply(remove_punctuation) In [28]: df &Panbsp; Out[28]: a b 0 cd edc.rc 1 3 3 2 d ef &nbndsp; 4
技能8:cut 数据分箱
将百分制分数转为A,B,C,D四个等级,bins 被分为 [0,60,75,90,100],labels 等于['D', 'C', 'B', 'A']:
# 生成20个[0,100]的随机整数 In [30]: a = np.random.randint(1,100,20) In [31]: a  as; Out[31]: array([48, 22, 46, 84, 13, 52, 36, 35, 27, 99, 31, 37, 15, 31, 5, 46, 98,99, 60,&nb实现sp;43]) # cut分箱 In [33]: pd.cut(a, [0,60,75,90,100], labels = ['D', 'C', 'B', 'A']) Out[33]: [D, D, D, B, D, ..., D, A, A, D, D] Length: 20 Categories数据 (4, object): [D < C < B < A]
技能9:rank 排名
rank 方法,生成数值排名,ascending 为False,分值越大,排名越靠前:
In [36]: df = pd.DataFrame({'a':[46, 98,99, 60, 43]} )) In [53]: df['a'].rank(ascending=False) &预处nbsp; Out[53]: 0 4.0 1 2.0 2 1.0 3 3.0 4 5.0
技能10:category列转数值
某列取值只可能为有限个枚举值,往往需要转为数值,使用get_dummies,或自己定义函数:
pd.get_dummies(df['a'])
自定义函数,结合 apply:
def c2n(x): 理的 if x=='A': return 95 if x=='B': return 80 df['a'].apply(c2n)
以上结合数据预处理的十个小任务,分别找到对应pandas中的实现。
Tags:
相关文章
可溶于水的机器人见过吗?明胶和糖3D打印而成
热点本文经AI新媒体量子位(公众号ID:QbitAI)授权转载,转载请联系出处。 有这么一个能溶于水的机器人,最近登上了Science子刊,还是封面: 没错,就是身上装了压力感应器,可用游戏手柄操控的正经...
【热点】
阅读更多快手72B代码模型开源登顶:KAT-Dev横扫SWE-Bench,国产AI编程迎来突破
热点国产AI在代码生成领域再次取得重大突破。快手旗下Kwaipilot团队近日正式开源KAT-Dev-72B-Exp模型,这款拥有720亿参数的实验性大语言模型凭借在软件工程基准测试中的出色表现,登顶开源...
【热点】
阅读更多吉祥彩
热点前言:吉祥彩是什么吉祥彩当然是人们非常充满着吉祥可爱,五彩缤纷的一种颜色。大家都喜欢和和美美而显得充满着吉祥。吉祥彩怎么样?很不错的,是菲律宾政\\\府指定机构颁发的牌照,很受欢迎的公司。吉祥彩单号最...
【热点】
阅读更多
热门文章
最新文章
友情链接
- 标签: 康师傅
- https://www.mesec.cz/vyhledavani/?qs=谷歌maps群发程序|软件【tg:seogoogle987】,Og0
- https://www.polliwogs.com/search/谷歌maps群发程序|软件【tg:seogoogle987】,ZNf
- https://www.therapeuticassociates.com/?cat=5&s=谷歌maps群发程序|软件【tg:seogoogle987】,OaC
- https://www.paspaley.com/catalogsearch/result/?action=QUERY&q=谷歌maps群发程序|软件【tg:seogoogle987】,igh
- https://www.kelsa.co.uk/catalogsearch/result/index/?T=1686132119291&q=谷歌maps群发程序|软件【tg:seogoogle987】,PHJ
- https://www.nytimes.com/search?dropmab=true&query=谷歌maps群发程序|软件【tg:seogoogle987】,Sjr
- https://www.yli.ro/catalogsearch/result/?q=谷歌maps群发程序|软件【tg:seogoogle987】,Vtx
- https://www.qylaws.com/search/谷歌maps群发程序|软件【tg:seogoogle987】,rFh
- https://www.luxurysociety.com/en/search?q=谷歌maps群发程序|软件【tg:seogoogle987】,svJ
- https://search.danawa.com/dsearch.php?query=谷歌maps群发程序|软件【tg:seogoogle987】GFK
- https://www.l-and-b.jp/?page=1&s=谷歌maps群发程序|软件【tg:seogoogle987】,RBM
- https://www.pearson.com/en-us/search.html?aq=谷歌maps群发程序|软件【tg:seogoogle987】,ILI
- https://www.safetv.go.kr/base/search/view?searchWord=谷歌maps群发程序|软件【tg:seogoogle987】eft
- https://www.rookwinkel.nl/catalogsearch/result/index/?q=谷歌maps群发程序|软件【tg:seogoogle987】,NSu
- https://www.yidian51.com/taobao/谷歌maps群发程序|软件【tg:seogoogle987】,Lap
- https://www.smics.com/tc/site/search?token=谷歌maps群发程序|软件【tg:seogoogle987】,vb8
- https://www.nbm.org/?action=QUERY&s=谷歌maps群发程序|软件【tg:seogoogle987】,UNN
- https://www.trockenmittel.ch/catalogsearch/result/?iC=ZM3&q=谷歌maps群发程序|软件【tg:seogoogle987】,fJd
- https://5gaa.org/?token=6e2l7hjt2i26gzgctgwdrv7v0j6nxorq&s=谷歌maps群发程序|软件【tg:seogoogle987】Qa6
- https://www.military-history.org/?s=谷歌maps群发程序|软件【tg:seogoogle987】,sFw
- https://www.mexperience.com/?T=1686132112324&s=谷歌maps群发程序|软件【tg:seogoogle987】,xvP
- https://www.navylookout.com/?ID=nSBR6Ivdu6lZg1yr2SDs30Lo5sTs76yJ&s=谷歌maps群发程序|软件【tg:seogoogle987】,gDC
- https://www.pearlstonecenter.org/search/谷歌maps群发程序|软件【tg:seogoogle987】,EFv
- https://www.universal-music.co.jp/search/?q=谷歌maps群发程序|软件【tg:seogoogle987】,jlP
- https://www.shacknews.com/editorial-search?q=谷歌maps群发程序|软件【tg:seogoogle987】,3yo
- https://www.londontheatredirect.com/search/谷歌maps群发程序|软件【tg:seogoogle987】,7uI
- https://www.vfw.org/search-results?q=谷歌maps群发程序|软件【tg:seogoogle987】,15B
- https://www.wheels24.gr/?action=CHECK&s=谷歌maps群发程序|软件【tg:seogoogle987】,sad
- https://www.schenckprocess.com/search?q=谷歌maps群发程序|软件【tg:seogoogle987】,p92
- https://www.teamlab.art/it/?submit=谷歌maps群发程序|软件【tg:seogoogle987】,qF1
- https://www.lgnewsroom.com/search/谷歌maps群发程序|软件【tg:seogoogle987】,fqm
- https://www.synonyms.com/antonyms/谷歌maps群发程序|软件【tg:seogoogle987】,QER
- https://www.safetv.go.kr/base/search/view?searchWord=谷歌maps群发程序|软件【tg:seogoogle987】JhE
- https://www.lneg.pt/?cat=140&s=谷歌maps群发程序|软件【tg:seogoogle987】,MAr
- https://xehay.vn/tim-kiem/=谷歌maps群发程序|软件【tg:seogoogle987】,G91
- https://www.reallywildbirdfood.co.uk/store/search?q=谷歌maps群发程序|软件【tg:seogoogle987】,rzE
- https://www.smarty.net/search?q=谷歌maps群发程序|软件【tg:seogoogle987】,tr2
- https://nicolazzi.it/en/?s=谷歌maps群发程序|软件【tg:seogoogle987】,2RS
- https://www.rhymezone.com/r/rhyme.cgi?typeofrhyme=oldrel&loc=nojs&Word=谷歌maps群发程序|软件【tg:seogoogle987】,YpM
- https://www.quarterjacksurgery.co.uk/?AT=3rBy&s=谷歌maps群发程序|软件【tg:seogoogle987】,8sC
- https://www.routesonline.com/search/?query=谷歌maps群发程序|软件【tg:seogoogle987】,HLt
- https://www.stonerealestate.com.au/?cat=100&s=谷歌maps群发程序|软件【tg:seogoogle987】,RMq
- https://www2.ual.es/ProyectoLates/?s=谷歌maps群发程序|软件【tg:seogoogle987】,6Vd
- https://www.lee.com/search?q=谷歌maps群发程序|软件【tg:seogoogle987】,Ul2
- https://www.shopping.com/search.html?keyword=谷歌maps群发程序|软件【tg:seogoogle987】,PVq
- https://m.shoppinghow.kakao.com/m/search/q/谷歌maps群发程序|软件【tg:seogoogle987】AGr
- https://www.maigoo.com/search/?block=&q=谷歌maps群发程序|软件【tg:seogoogle987】,sz8
- https://www.unclefossil.com/search/谷歌maps群发程序|软件【tg:seogoogle987】gad_source=1&gclid=Cj0KCQjw_-GxBhC1ARIsADGgDjsNKplUXa2F6YGlHBK2sHDQ88cgsJBb05DyO99TV9jNq_cT0kGMqyQaAgHQEALw_wcB
- https://www.zutari.com/?s=谷歌maps群发程序|软件【tg:seogoogle987】,ySJ
- https://www.flickr.com/photos/tags/谷歌maps群发程序|软件【tg:seogoogle987】3Jo