亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時間:2023年08月24日 09:55:57   作者:sodaloveer  
這篇文章主要介紹了Python數(shù)據(jù)清洗之利用pandas篩選數(shù)據(jù)詳解,Pandas是一個用于數(shù)據(jù)分析和處理的Python庫,它提供了高效的數(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對象,用于存儲任意類型的數(shù)據(jù)。

DataFrame是二維的表格型數(shù)據(jù)結(jié)構(gòu),可以存儲多種類型的數(shù)據(jù),并且可以進行靈活的數(shù)據(jù)操作和分析。

直接篩選

比較運算符(==、<、>、>=、<=、!=)邏輯運算符 &(與)、|(或)、~(非),使用比較運算符時,請將每個條件括在括號內(nèi)。

運算符的優(yōu)先級是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] ##用比較運算符‘=='直接篩選

篩選出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] ##用比較運算符“>=”直接篩選

篩選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))]

范圍運算 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)

測試pattern或regex是否包含在Series或Index的字符串中。

Series列要為字符數(shù)據(jù)類型。

最終返回:布爾值的系列或索引。

布爾值的Series或Index,指示給定模式是否包含在Series或Index的每個元素的字符串中。

函數(shù)語法:

Series.str.contains(pat,case = True,flags = 0,na = nan,regex = True)

參數(shù)說明如下:

參數(shù)描述
patstr類型。字符序列或正則表達式。
casebool,默認為True。如果為True,區(qū)分大小寫。
flagsint,默認為0(無標(biāo)志)。標(biāo)志傳遞到re模塊,例如re.IGNORECASE。
na默認NaN,填寫缺失值的值。
regexbool,默認為True。如果為True,則假定pat是正則表達式。如果為False,則將pat視為文字字符串。所以針對特殊符號,默認情況下我們必須使用轉(zhuǎn)義符,或者設(shè)置 regex=False。

篩選billing_nbr為移動號碼,移動號碼用正則表達式

#該列轉(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)

模糊查詢,篩選某列中包含某個字符,比如“篩選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ù)據(jù),na=False的意思就是,遇到非字符串的情況,直接忽略。你也可以寫na=True,意思就是遇到非字符串的情況,計為篩選有效。如果遇到非字符串沒有標(biāo)明na參數(shù)會報錯。

data_pos_1=data_pos_1[data_pos_1['channel_type_desc'].str.contains('實體渠道',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為移動號碼,移動號碼用正則表達式

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ù)據(jù)詳情:

在這里插入圖片描述

import re 
def func(data):
    if re.match(r'[\u4e00-\u9fa5]*實體渠道*[\u4e00-\u9fa5]',str(data)):
        return "實體渠道"
    elif re.match(r'[\u4e00-\u9fa5]*電子渠道*[\u4e00-\u9fa5]',str(data)):
        return "電子渠道"
    elif re.match(r'[\u4e00-\u9fa5]*直銷渠道*[\u4e00-\u9fa5]',str(data)):
        return "直銷渠道"
    else:
        return "未識別"
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]*實體渠道*[\u4e00-\u9fa5]',str(x)):
        return "實體渠道"
    elif re.search(r'[\u4e00-\u9fa5]*電子渠道*[\u4e00-\u9fa5]',str(x)):
        return "電子渠道"
    elif re.search(r'[\u4e00-\u9fa5]*直銷渠道*[\u4e00-\u9fa5]',str(x)):
        return "直銷渠道"
    else:
        return "未識別"
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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python實現(xiàn)在sqlite動態(tài)創(chuàng)建表的方法

    python實現(xiàn)在sqlite動態(tài)創(chuàng)建表的方法

    這篇文章主要介紹了python實現(xiàn)在sqlite動態(tài)創(chuàng)建表的方法,涉及Python操作SQLite數(shù)據(jù)庫創(chuàng)建數(shù)據(jù)表的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-05-05
  • python去除字符串中空格的6種常用方法

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

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

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

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

    Python搭建Gitee圖床的示例代碼

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

    Python 3.6打包成EXE可執(zhí)行程序的實現(xiàn)

    這篇文章主要介紹了Python 3.6打包成EXE可執(zhí)行程序的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • python去除所有html標(biāo)簽的方法

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

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

    Python編寫Windows Service服務(wù)程序

    這篇文章主要為大家詳細介紹了Python編寫Windows Service服務(wù)程序,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • 詳解python websocket獲取實時數(shù)據(jù)的幾種常見鏈接方式

    詳解python websocket獲取實時數(shù)據(jù)的幾種常見鏈接方式

    這篇文章主要介紹了詳解python websocket獲取實時數(shù)據(jù)的幾種常見鏈接方式,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • 用yum安裝MySQLdb模塊的步驟方法

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

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

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

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

最新評論