Python數(shù)據(jù)清洗之利用pandas篩選數(shù)據(jù)詳解
利用pandas篩選數(shù)據(jù)
在Pandas中,最常用的數(shù)據(jù)結(jié)構(gòu)是Series和DataFrame。
Series是一維的數(shù)組-like對(duì)象,用于存儲(chǔ)任意類型的數(shù)據(jù)。
DataFrame是二維的表格型數(shù)據(jù)結(jié)構(gòu),可以存儲(chǔ)多種類型的數(shù)據(jù),并且可以進(jìn)行靈活的數(shù)據(jù)操作和分析。
直接篩選
比較運(yùn)算符(==、<、>、>=、<=、!=)邏輯運(yùn)算符 &(與)、|(或)、~(非),使用比較運(yùn)算符時(shí),請(qǐng)將每個(gè)條件括在括號(hào)內(nèi)。
運(yùn)算符的優(yōu)先級(jí)是NOT(?),AND(&),OR(|)。
讀取數(shù)據(jù)
import os import pandas as pd import numpy as np #讀取文件 def read_file(filepath): os.chdir(os.path.dirname(filepath)) return pd.read_csv(os.path.basename(filepath),encoding='utf-8') file_pos="C:\\Users\\周曉婷\\Desktop\\train_data_original.csv" data_pos=read_file(file_pos)
查看數(shù)據(jù)類型
data_pos.dtypes
篩選出frand_flag為0的數(shù)據(jù)
data_pos[data_pos['frand_flag']==0] ##用比較運(yùn)算符‘=='直接篩選
篩選出frand_flag不為1的數(shù)據(jù)
data_pos[data_pos['frand_flag']!=1] data_pos[~(data_pos['frand_flag']==1)]
篩選cdr_duration>=15的數(shù)據(jù)
data_pos[data_pos['cdr_duration']>=15] ##用比較運(yùn)算符“>=”直接篩選
篩選cdr_duration<15的數(shù)據(jù)
data_pos[data_pos['cdr_duration']<15]
篩選cdr_duration<=15且frand_flag=0的數(shù)據(jù)
data_pos[(data_pos['cdr_duration']<=15)&(data_pos['frand_flag']==0)]
篩選cdr_duration<=15或cdr_duration>60的數(shù)據(jù)
data_pos[(data_pos['cdr_duration']<=15)|(data_pos['cdr_duration']>60)]
函數(shù)篩選
比較函數(shù)(eq, ne, le, lt, ge, gt)
篩選出frand_flag為0的數(shù)據(jù)
data_pos[data_pos['frand_flag'].eq(0)]
篩選出frand_flag不為1的數(shù)據(jù)
data_pos[data_pos['frand_flag'].ne(1)]
篩選cdr_duration>=15的數(shù)據(jù)
data_pos[data_pos['cdr_duration'].ge(15)]
篩選cdr_duration<=15的數(shù)據(jù)
data_pos[data_pos['cdr_duration'].le(15)]
篩選cdr_duration<=15且frand_flag=0的數(shù)據(jù)
data_pos[(data_pos['cdr_duration'].le(15))&(data_pos['frand_flag'].eq(0))]
范圍運(yùn)算 between(left,right)
篩選cdr_duration>=15或cdr_duration<=60的數(shù)據(jù)
data_pos[data_pos['cdr_duration'].between(15,60)]
篩選start_date>=20220701且start_date<=20221031的數(shù)據(jù)
data_pos[data_pos['start_date'].between(20220701,20221031)]
字符篩選 Series.str.contains(pattern或字符串,na=False)
測(cè)試pattern或regex是否包含在Series或Index的字符串中。
Series列要為字符數(shù)據(jù)類型。
最終返回:布爾值的系列或索引。
布爾值的Series或Index,指示給定模式是否包含在Series或Index的每個(gè)元素的字符串中。
函數(shù)語(yǔ)法:
Series.str.contains(pat,case = True,flags = 0,na = nan,regex = True)
參數(shù)說(shuō)明如下:
參數(shù) | 描述 |
pat | str類型。字符序列或正則表達(dá)式。 |
case | bool,默認(rèn)為True。如果為True,區(qū)分大小寫。 |
flags | int,默認(rèn)為0(無(wú)標(biāo)志)。標(biāo)志傳遞到re模塊,例如re.IGNORECASE。 |
na | 默認(rèn)NaN,填寫缺失值的值。 |
regex | bool,默認(rèn)為True。如果為True,則假定pat是正則表達(dá)式。如果為False,則將pat視為文字字符串。所以針對(duì)特殊符號(hào),默認(rèn)情況下我們必須使用轉(zhuǎn)義符,或者設(shè)置 regex=False。 |
篩選billing_nbr為移動(dòng)號(hào)碼,移動(dòng)號(hào)碼用正則表達(dá)式
#該列轉(zhuǎn)換為字符數(shù)據(jù)類型(2種方法) data_pos['billing_nbr']=data_pos['billing_nbr'].apply(str) data_pos['billing_nbr']=data_pos['billing_nbr'].values.astype('str') data_pos=data_pos[data_pos['billing_nbr'].str.contains('^1[35678]\d{9}$')] print(data_pos.shape)
模糊查詢,篩選某列中包含某個(gè)字符,比如“篩選start_date為202207的數(shù)據(jù)”
data_pos['start_date']=data_pos['start_date'].apply(str) data_pos=data_pos[data_pos['start_date'].str.contains('202207')] print(data_pos.shape)
篩選channel_type_desc為實(shí)體渠道的數(shù)據(jù),na=False的意思就是,遇到非字符串的情況,直接忽略。你也可以寫na=True,意思就是遇到非字符串的情況,計(jì)為篩選有效。如果遇到非字符串沒(méi)有標(biāo)明na參數(shù)會(huì)報(bào)錯(cuò)。
data_pos_1=data_pos_1[data_pos_1['channel_type_desc'].str.contains('實(shí)體渠道',na=False)]
apply()函數(shù)
篩選出frand_flag為0的數(shù)據(jù)
data_pos[data_pos['frand_flag'].apply(lambda x:x==0)]
截取billing_nbr前7位數(shù)
data_pos['billing_nbr']=data_pos['billing_nbr'].apply(str) data_pos['billing_nbr_pre7']=data_pos['billing_nbr'].apply(lambda x:x[0:8]) data_pos['billing_nbr_pre7']=data_pos['billing_nbr'].map(lambda x:x[0:8])
篩選billing_nbr為移動(dòng)號(hào)碼,移動(dòng)號(hào)碼用正則表達(dá)式
import re def func(x): if re.search('^1[35678]\d{9}$',x): return(True) else: return(False) data_pos[data_pos['billing_nbr'].apply(func)]
篩選某一列并替換其他字符:篩選channel_type_desc列,將”含有實(shí)體渠道的“全部替換”實(shí)體渠道”,將“含有電子渠道的”全部替換成“電子渠道”,將“含有直銷渠道的”全部替換成“直銷渠道”,其他替換為未知。
未修改前,數(shù)據(jù)詳情:
import re def func(data): if re.match(r'[\u4e00-\u9fa5]*實(shí)體渠道*[\u4e00-\u9fa5]',str(data)): return "實(shí)體渠道" elif re.match(r'[\u4e00-\u9fa5]*電子渠道*[\u4e00-\u9fa5]',str(data)): return "電子渠道" elif re.match(r'[\u4e00-\u9fa5]*直銷渠道*[\u4e00-\u9fa5]',str(data)): return "直銷渠道" else: return "未識(shí)別" data_pos_1['channel_type_desc_1']=data_pos_1.channel_type_desc.apply(func)
import re def func(x): if re.search(r'[\u4e00-\u9fa5]*實(shí)體渠道*[\u4e00-\u9fa5]',str(x)): return "實(shí)體渠道" elif re.search(r'[\u4e00-\u9fa5]*電子渠道*[\u4e00-\u9fa5]',str(x)): return "電子渠道" elif re.search(r'[\u4e00-\u9fa5]*直銷渠道*[\u4e00-\u9fa5]',str(x)): return "直銷渠道" else: return "未識(shí)別" data_pos_1['channel_type_desc_1']=data_pos_1.channel_type_desc.apply(func)
數(shù)據(jù)替代后,數(shù)據(jù)詳情:
isin()函數(shù),支持多值篩選,用列表表示
篩選出frand_flag為0的數(shù)據(jù)
data_pos[data_pos['frand_flag'].isin([0])]
篩選出called_nbr包含10086、10010、10016、114的數(shù)據(jù)
data_pos[data_pos['called_nbr'].isin([10086,10010,10016,114])]
~isin()
篩選called_nbr不包含10086、10010、10016、114的數(shù)據(jù)
data_pos[~data_pos['called_nbr'].isin([10086,10010,10016,114])]
到此這篇關(guān)于Python數(shù)據(jù)清洗之利用pandas篩選數(shù)據(jù)詳解的文章就介紹到這了,更多相關(guān)pandas篩選數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Pandas 數(shù)據(jù)處理,數(shù)據(jù)清洗詳解
- 利用pandas進(jìn)行數(shù)據(jù)清洗的方法
- pandas數(shù)據(jù)清洗實(shí)現(xiàn)刪除的項(xiàng)目實(shí)踐
- Pandas數(shù)據(jù)清洗函數(shù)總結(jié)
- Pandas數(shù)據(jù)清洗的實(shí)現(xiàn)
- 利用pandas進(jìn)行數(shù)據(jù)清洗的7種方式
- 基于pandas數(shù)據(jù)清洗的實(shí)現(xiàn)示例
- Pandas數(shù)據(jù)清洗與過(guò)濾空值技巧
- Pandas數(shù)據(jù)清洗的維度詳解
- Pandas 數(shù)據(jù)清洗的具體使用
相關(guān)文章
python實(shí)現(xiàn)在sqlite動(dòng)態(tài)創(chuàng)建表的方法
這篇文章主要介紹了python實(shí)現(xiàn)在sqlite動(dòng)態(tài)創(chuàng)建表的方法,涉及Python操作SQLite數(shù)據(jù)庫(kù)創(chuàng)建數(shù)據(jù)表的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-05-05用python處理圖片實(shí)現(xiàn)圖像中的像素訪問(wèn)
本篇文章主要介紹了用python處理圖片實(shí)現(xiàn)圖像中的像素訪問(wèn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05Python 3.6打包成EXE可執(zhí)行程序的實(shí)現(xiàn)
這篇文章主要介紹了Python 3.6打包成EXE可執(zhí)行程序的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10Python編寫Windows Service服務(wù)程序
這篇文章主要為大家詳細(xì)介紹了Python編寫Windows Service服務(wù)程序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01詳解python websocket獲取實(shí)時(shí)數(shù)據(jù)的幾種常見(jiàn)鏈接方式
這篇文章主要介紹了詳解python websocket獲取實(shí)時(shí)數(shù)據(jù)的幾種常見(jiàn)鏈接方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07