8段用于數(shù)據(jù)清洗Python代碼(小結(jié))
最近,大數(shù)據(jù)工程師Kin Lim Lee在Medium上發(fā)表了一篇文章,介紹了8個用于數(shù)據(jù)清洗的Python代碼。
數(shù)據(jù)清洗,是進(jìn)行數(shù)據(jù)分析和使用數(shù)據(jù)訓(xùn)練模型的必經(jīng)之路,也是最耗費(fèi)數(shù)據(jù)科學(xué)家/程序員精力的地方。
這些用于數(shù)據(jù)清洗的代碼有兩個優(yōu)點(diǎn):一是由函數(shù)編寫而成,不用改參數(shù)就可以直接使用。二是非常簡單,加上注釋最長的也不過11行。在介紹每一段代碼時,Lee都給出了用途,也在代碼中也給出注釋。大家可以把這篇文章收藏起來,當(dāng)做工具箱使用。
涵蓋8大場景的數(shù)據(jù)清洗代碼
這些數(shù)據(jù)清洗代碼,一共涵蓋8個場景,分別是:
刪除多列、更改數(shù)據(jù)類型、將分類變量轉(zhuǎn)換為數(shù)字變量、檢查缺失數(shù)據(jù)、刪除列中的字符串、刪除列中的空格、用字符串連接兩列(帶條件)、轉(zhuǎn)換時間戳(從字符串到日期時間格式)
刪除多列
在進(jìn)行數(shù)據(jù)分析時,并非所有的列都有用,用df.drop可以方便地刪除你指定的列。
def drop_multiple_col(col_names_list, df): AIM -> Drop multiple columns based on their column names INPUT -> List of column names, df OUTPUT -> updated df with dropped columns ------ df.drop(col_names_list, axis=1, inplace=True) return df
轉(zhuǎn)換數(shù)據(jù)類型
當(dāng)數(shù)據(jù)集變大時,需要轉(zhuǎn)換數(shù)據(jù)類型來節(jié)省內(nèi)存。
def change_dtypes(col_int, col_float, df): AIM -> Changing dtypes to save memory INPUT -> List of column names (int, float), df OUTPUT -> updated df with smaller memory ------ df[col_int] = df[col_int].astype( int32 ) df[col_float] = df[col_float].astype( float32 )
將分類變量轉(zhuǎn)換為數(shù)值變量
一些機(jī)器學(xué)習(xí)模型要求變量采用數(shù)值格式。這需要先將分類變量轉(zhuǎn)換為數(shù)值變量。同時,你也可以保留分類變量,以便進(jìn)行數(shù)據(jù)可視化。
def convert_cat2num(df): # Convert categorical variable to numerical variable num_encode = { col_1 : { YES :1, NO :0}, col_2 : { WON :1, LOSE :0, DRAW :0}} df.replace(num_encode, inplace=True)
檢查缺失數(shù)據(jù)
如果你要檢查每列缺失數(shù)據(jù)的數(shù)量,使用下列代碼是最快的方法??梢宰屇愀玫亓私饽男┝腥笔У臄?shù)據(jù)更多,從而確定怎么進(jìn)行下一步的數(shù)據(jù)清洗和分析操作。
def check_missing_data(df): # check for any missing data in the df (display in descending order) return df.isnull().sum().sort_values(ascending=False)
刪除列中的字符串
有時候,會有新的字符或者其他奇怪的符號出現(xiàn)在字符串列中,這可以使用df[‘col_1'].replace很簡單地把它們處理掉。
def remove_col_str(df): # remove a portion of string in a dataframe column - col_1 df[ col_1 ].replace(, , regex=True, inplace=True) # remove all the characters after &# (including &#) for column - col_1 df[ col_1 ].replace( &#.* , , regex=True, inplace=True)
刪除列中的空格
數(shù)據(jù)混亂的時候,什么情況都有可能發(fā)生。字符串開頭經(jīng)常會有一些空格。在刪除列中字符串開頭的空格時,下面的代碼非常有用。
def remove_col_white_space(df): # remove white space at the beginning of string df[col] = df[col].str.lstrip()
用字符串連接兩列(帶條件)
當(dāng)你想要有條件地用字符串將兩列連接在一起時,這段代碼很有幫助。比如,你可以在第一列結(jié)尾處設(shè)定某些字母,然后用它們與第二列連接在一起。根據(jù)需要,結(jié)尾處的字母也可以在連接完成后刪除。
def concat_col_str_condition(df): # concat 2 columns with strings if the last 3 letters of the first column are pil mask = df[ col_1 ].str.endswith( pil , na=False) col_new = df[mask][ col_1 ] + df[mask][ col_2 ] col_new.replace( pil , , regex=True, inplace=True) # replace the pil with emtpy space
轉(zhuǎn)換時間戳(從字符串到日期時間格式)
在處理時間序列數(shù)據(jù)時,我們很可能會遇到字符串格式的時間戳列。這意味著要將字符串格式轉(zhuǎn)換為日期時間格式(或者其他根據(jù)我們的需求指定的格式) ,以便對數(shù)據(jù)進(jìn)行有意義的分析。
def convert_str_datetime(df): AIM -> Convert datetime(String) to datetime(format we want) INPUT -> df OUTPUT -> updated df with new datetime format ------ df.insert(loc=2, column= timestamp , value=pd.to_datetime(df.transdate, format= %Y-%m-%d
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 用Python實(shí)現(xiàn)網(wǎng)易云音樂的數(shù)據(jù)進(jìn)行數(shù)據(jù)清洗和可視化分析
- Python數(shù)據(jù)清洗工具之Numpy的基本操作
- python 利用已有Ner模型進(jìn)行數(shù)據(jù)清洗合并代碼
- python實(shí)現(xiàn)數(shù)據(jù)清洗(缺失值與異常值處理)
- python3常用的數(shù)據(jù)清洗方法(小結(jié))
- 對python數(shù)據(jù)清洗容易遇到的函數(shù)-re.sub bytes string詳解
- python數(shù)據(jù)清洗系列之字符串處理詳解
- python 數(shù)據(jù)清洗之?dāng)?shù)據(jù)合并、轉(zhuǎn)換、過濾、排序
- Python?八個數(shù)據(jù)清洗實(shí)例代碼詳解
相關(guān)文章
Django框架HttpRequest對象用法實(shí)例分析
這篇文章主要介紹了Django框架HttpRequest對象用法,結(jié)合實(shí)例形式分析了Django框架HttpRequest對象發(fā)送請求數(shù)據(jù)的相關(guān)使用技巧,需要的朋友可以參考下2019-11-11python密碼學(xué)周期置換密碼學(xué)習(xí)
這篇文章主要為大家介紹了python密碼學(xué)周期置換密碼的學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05PyCharm vs VSCode,作為python開發(fā)者,你更傾向哪種IDE呢?
這篇文章主要介紹了PyCharm和vscode作為python ide的優(yōu)劣,幫助你選擇適合自己的ide,感興趣的朋友可以了解下2020-08-08python游戲開發(fā)之視頻轉(zhuǎn)彩色字符動畫
這篇文章主要為大家詳細(xì)介紹了python游戲開發(fā)之視頻轉(zhuǎn)彩色字符動畫,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-04-04Tensorflow實(shí)現(xiàn)部分參數(shù)梯度更新操作
今天小編就為大家分享一篇Tensorflow實(shí)現(xiàn)部分參數(shù)梯度更新操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01python實(shí)現(xiàn)定時發(fā)送郵件到指定郵箱
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)定時發(fā)送郵件到指定郵箱,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-12-12分析運(yùn)行中的 Python 進(jìn)程詳細(xì)解析
這篇文章主要介紹了分析運(yùn)行中的 Python 進(jìn)程,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-06-06Python 動態(tài)導(dǎo)入對象,importlib.import_module()的使用方法
今天小編就為大家分享一篇Python 動態(tài)導(dǎo)入對象,importlib.import_module()的使用方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08