欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python數(shù)據(jù)清洗之利用pandas篩選數(shù)據(jù)詳解

 更新時(shí)間:2023年08月24日 09:55:57   作者:sodaloveer  
這篇文章主要介紹了Python數(shù)據(jù)清洗之利用pandas篩選數(shù)據(jù)詳解,Pandas是一個(gè)用于數(shù)據(jù)分析和處理的Python庫(kù),它提供了高效的數(shù)據(jù)結(jié)構(gòu)和數(shù)據(jù)分析工具,使得數(shù)據(jù)的清洗、轉(zhuǎn)換、分析和可視化變得更加容易和靈活,需要的朋友可以參考下

利用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ù)描述
patstr類型。字符序列或正則表達(dá)式。
casebool,默認(rèn)為True。如果為True,區(qū)分大小寫。
flagsint,默認(rèn)為0(無(wú)標(biāo)志)。標(biāo)志傳遞到re模塊,例如re.IGNORECASE。
na默認(rèn)NaN,填寫缺失值的值。
regexbool,默認(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)文章希望大家以后多多支持腳本之家!

相關(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實(shí)現(xiàn)在sqlite動(dòng)態(tài)創(chuàng)建表的方法,涉及Python操作SQLite數(shù)據(jù)庫(kù)創(chuàng)建數(shù)據(jù)表的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-05-05
  • python去除字符串中空格的6種常用方法

    python去除字符串中空格的6種常用方法

    最近業(yè)務(wù)需要對(duì)Pyhon中的一些字符串內(nèi)容去除空格,方便后續(xù)處理,下面這篇文章主要給大家介紹了關(guān)于python去除字符串中空格的6種常用方法,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05
  • 用python處理圖片實(shí)現(xiàn)圖像中的像素訪問(wèn)

    用python處理圖片實(shí)現(xiàn)圖像中的像素訪問(wèn)

    本篇文章主要介紹了用python處理圖片實(shí)現(xiàn)圖像中的像素訪問(wèn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-05-05
  • Python搭建Gitee圖床的示例代碼

    Python搭建Gitee圖床的示例代碼

    在寫博客的過(guò)程中經(jīng)常要插入圖片,本文將使用Python實(shí)現(xiàn)對(duì)上傳的圖片自動(dòng)壓縮,自動(dòng)編碼,以及自動(dòng)推送到遠(yuǎn)程倉(cāng)庫(kù),感興趣的可以了解一下
    2021-10-10
  • Python 3.6打包成EXE可執(zhí)行程序的實(shí)現(xiàn)

    Python 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-10
  • python去除所有html標(biāo)簽的方法

    python去除所有html標(biāo)簽的方法

    這篇文章主要介紹了python去除所有html標(biāo)簽的方法,涉及Python正則替換的相關(guān)技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下
    2015-05-05
  • Python編寫Windows Service服務(wù)程序

    Python編寫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)鏈接方式

    這篇文章主要介紹了詳解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
  • 用yum安裝MySQLdb模塊的步驟方法

    用yum安裝MySQLdb模塊的步驟方法

    在python2.7版本中,MySQLdb模塊還不是python的內(nèi)置模塊,但是MySQLdb模塊又是Python與MySQL連接的橋梁,對(duì)于作為MySQL DBA又很喜歡Python語(yǔ)言的我來(lái)說(shuō),MySQLdb真的是必需品呢。所以就需要自己進(jìn)行安裝了,這篇文章就給大家詳細(xì)介紹了關(guān)于用yum安裝MySQLdb模塊的步驟。
    2016-12-12
  • python密碼學(xué)RSA密碼解密教程

    python密碼學(xué)RSA密碼解密教程

    這篇文章主要為大家介紹了python密碼學(xué)RSA密碼解密教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05

最新評(píng)論