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)文章
pytorch中nn.Flatten()函數(shù)詳解及示例
nn.Flatten是一個(gè)類,而torch.flatten()則是一個(gè)函數(shù),下面這篇文章主要給大家介紹了關(guān)于pytorch中nn.Flatten()函數(shù)詳解及示例的相關(guān)資料,需要的朋友可以參考下2023-01-01
Python異步編程從協(xié)程到異步框架實(shí)踐指南
Python的異步編程提供了一種更輕量級(jí)的并發(fā)方案,能夠在單線程內(nèi)實(shí)現(xiàn)高并發(fā),大幅提升I/O密集型應(yīng)用的性能,本文將從協(xié)程的基礎(chǔ)概念講起,深入講解asyncio的核心原理和實(shí)戰(zhàn)應(yīng)用,幫助讀者建立完整的異步編程知識(shí)體系2026-05-05
基于Tensorflow使用CPU而不用GPU問(wèn)題的解決
今天小編就為大家分享一篇基于Tensorflow使用CPU而不用GPU問(wèn)題的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02
詳解Python如何解析JSON中的對(duì)象數(shù)組
這篇文章主要為大家詳細(xì)介紹了如何使用Python的JSON模塊傳輸和接收J(rèn)SON數(shù)據(jù),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-10-10
django 捕獲異常和日志系統(tǒng)過(guò)程詳解
這篇文章主要介紹了django-捕獲異常和日志系統(tǒng)過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07

