自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

還在為數(shù)據(jù)清洗抓狂?這里有一個(gè)簡(jiǎn)單實(shí)用的清洗代碼集

開(kāi)發(fā) 開(kāi)發(fā)工具 前端
數(shù)據(jù)清洗是數(shù)據(jù)科學(xué)家逃不掉的一份苦差事。為了讓這項(xiàng)工作不那么痛苦,本文作者分享了自己的數(shù)據(jù)清洗代碼集。

現(xiàn)實(shí)世界中的數(shù)據(jù)通常質(zhì)量不高,作為一名數(shù)據(jù)科學(xué)家,有時(shí)也需要承擔(dān)一部分?jǐn)?shù)據(jù)清洗的工作,這要求數(shù)據(jù)科學(xué)家們應(yīng)該能夠在進(jìn)行數(shù)據(jù)分析或建模工作之前執(zhí)行數(shù)據(jù)清洗步驟,從而確保數(shù)據(jù)的質(zhì)量***。

[[256182]]

不過(guò)長(zhǎng)話(huà)短說(shuō),在數(shù)據(jù)科學(xué)領(lǐng)域工作了很長(zhǎng)一段時(shí)間后,我切實(shí)感受到了在進(jìn)行數(shù)據(jù)分析、可視化和建模工作之前,進(jìn)行數(shù)據(jù)清洗工作是多么痛苦。

不管你承不承認(rèn),數(shù)據(jù)清洗著實(shí)不是一件簡(jiǎn)單的任務(wù),大多數(shù)情況下這項(xiàng)工作是十分耗時(shí)而乏味的,但它又是十分重要的。

如果你經(jīng)歷過(guò)數(shù)據(jù)清洗的過(guò)程,你就會(huì)明白我的意思。而這正是撰寫(xiě)這篇文章的目的——讓讀者更輕松地進(jìn)行數(shù)據(jù)清洗工作。

事實(shí)上,我在不久前意識(shí)到,在進(jìn)行數(shù)據(jù)清洗時(shí),有一些數(shù)據(jù)具有相似的模式。也正是從那時(shí)起,我開(kāi)始整理并編譯了一些數(shù)據(jù)清洗代碼(見(jiàn)下文),我認(rèn)為這些代碼也適用于其它的常見(jiàn)場(chǎng)景。

由于這些常見(jiàn)的場(chǎng)景涉及到不同類(lèi)型的數(shù)據(jù)集,因此本文更加側(cè)重于展示和解釋這些代碼可以用于完成哪些工作,以便讀者更加方便地使用它們。

我的數(shù)據(jù)清洗小工具箱

在下面的代碼片段中,數(shù)據(jù)清洗代碼被封裝在了一些函數(shù)中,代碼的目的十分直觀。你可以直接使用這些代碼,無(wú)需將它們嵌入到需要進(jìn)行少量參數(shù)修改的函數(shù)中。

1. 刪除多列數(shù)據(jù)

  1. def drop_multiple_col(col_names_list, df):  
  2.     ''' 
  3.     AIM    -> Drop multiple columns based on their column names  
  4.  
  5.     INPUT  -> List of column names, df 
  6.  
  7.     OUTPUT -> updated df with dropped columns  
  8.     ------ 
  9.     ''' 
  10.     df.drop(col_names_list, axis=1inplace=True
  11.     return df 

有時(shí),并不是所有列的數(shù)據(jù)都對(duì)我們的數(shù)據(jù)分析工作有用。因此,「df.drop」可以方便地刪掉你選定的列。

2. 轉(zhuǎn)換 Dtypes

  1. def change_dtypes(col_int, col_float, df):  
  2.     ''' 
  3.     AIM    -> Changing dtypes to save memory 
  4.  
  5.     INPUT  -> List of column names (int, float), df 
  6.  
  7.     OUTPUT -> updated df with smaller memory   
  8.     ------ 
  9.     ''' 
  10.     df[col_int] = df[col_int].astype('int32') 
  11.     df[col_float] = df[col_float].astype('float32') 

當(dāng)我們面對(duì)更大的數(shù)據(jù)集時(shí),我們需要對(duì)「dtypes」進(jìn)行轉(zhuǎn)換,從而節(jié)省內(nèi)存。如果你有興趣學(xué)習(xí)如何使用「Pandas」來(lái)處理大數(shù)據(jù),我強(qiáng)烈推薦你閱讀「Why and How to Use Pandas with Large Data」這篇文章

(https://towardsdatascience.com/why-and-how-to-use-pandas-with-large-data-9594dda2ea4c)。

3. 將分類(lèi)變量轉(zhuǎn)換為數(shù)值變量

  1. def convert_cat2num(df): 
  2.     # Convert categorical variable to numerical variable 
  3.     num_encode = {'col_1' : {'YES':1, 'NO':0}, 
  4.                   'col_2'  : {'WON':1, 'LOSE':0, 'DRAW':0}}   
  5.     df.replace(num_encode, inplace=True)   

有一些機(jī)器學(xué)習(xí)模型要求變量是以數(shù)值形式存在的。這時(shí),我們就需要將分類(lèi)變量轉(zhuǎn)換成數(shù)值變量然后再將它們作為模型的輸入。對(duì)于數(shù)據(jù)可視化任務(wù)來(lái)說(shuō),我建議大家保留分類(lèi)變量,從而讓可視化結(jié)果有更明確的解釋?zhuān)阌诶斫狻?/p>

4. 檢查缺失的數(shù)據(jù)

  1. def check_missing_data(df): 
  2.     # check for any missing data in the df (display in descending order) 
  3.     return df.isnull().sum().sort_values(ascending=False

如果你想要檢查每一列中有多少缺失的數(shù)據(jù),這可能是最快的方法。這種方法可以讓你更清楚地知道哪些列有更多的缺失數(shù)據(jù),幫助你決定接下來(lái)在數(shù)據(jù)清洗和數(shù)據(jù)分析工作中應(yīng)該采取怎樣的行動(dòng)。

5. 刪除列中的字符串

  1. def remove_col_str(df): 
  2.     # remove a portion of string in a dataframe column - col_1 
  3.     df['col_1'].replace('\n', '', regex=Trueinplace=True
  4.  
  5.     # remove all the characters after &# (including &#) for column - col_1 
  6.     df['col_1'].replace(' &#.*', '', regex=Trueinplace=True

有時(shí)你可能會(huì)看到一行新的字符,或在字符串列中看到一些奇怪的符號(hào)。你可以很容易地使用 df['col_1'].replace 來(lái)處理該問(wèn)題,其中「col_1」是數(shù)據(jù)幀 df 中的一列。

6. 刪除列中的空格

  1. def remove_col_white_space(df): 
  2.     # remove white space at the beginning of string  
  3.     df[col] = df[col].str.lstrip() 

當(dāng)數(shù)據(jù)十分混亂時(shí),很多意想不到的情況都會(huì)發(fā)生。在字符串的開(kāi)頭有一些空格是很常見(jiàn)的。因此,當(dāng)你想要?jiǎng)h除列中字符串開(kāi)頭的空格時(shí),這種方法很實(shí)用。

7. 將兩列字符串?dāng)?shù)據(jù)(在一定條件下)拼接起來(lái)

  1. def concat_col_str_condition(df): 
  2.     # concat 2 columns with strings if the last 3 letters of the first column are 'pil' 
  3.     mask = df['col_1'].str.endswith('pil', na=False
  4.     col_new = df[mask]['col_1'] + df[mask]['col_2'] 
  5.     col_new.replace('pil', ' ', regex=Trueinplace=True)  # replace the 'pil' with emtpy space 

當(dāng)你希望在一定條件下將兩列字符串?dāng)?shù)據(jù)組合在一起時(shí),這種方法很有用。例如,你希望當(dāng)***列以某些特定的字母結(jié)尾時(shí),將***列和第二列數(shù)據(jù)拼接在一起。根據(jù)你的需要,還可以在拼接工作完成后將結(jié)尾的字母刪除掉。

8. 轉(zhuǎn)換時(shí)間戳(從字符串類(lèi)型轉(zhuǎn)換為日期「DateTime」格式)

  1. def convert_str_datetime(df):  
  2.     ''' 
  3.     AIM    -> Convert datetime(String) to datetime(format we want) 
  4.  
  5.     INPUT  -> df 
  6.  
  7.     OUTPUT -> updated df with new datetime format  
  8.     ------ 
  9.     ''' 
  10.     df.insert(loc=2column='timestamp'value=pd.to_datetime(df.transdate, format='%Y-%m-%d %H:%M:%S.%f')) 

在處理時(shí)間序列數(shù)據(jù)時(shí),你可能會(huì)遇到字符串格式的時(shí)間戳列。這意味著我們可能不得不將字符串格式的數(shù)據(jù)轉(zhuǎn)換為根據(jù)我們的需求指定的日期「datetime」格式,以便使用這些數(shù)據(jù)進(jìn)行有意義的分析和展示。

原文鏈接:

https://towardsdatascience.com/the-simple-yet-practical-data-cleaning-codes-ad27c4ce0a38

【本文是51CTO專(zhuān)欄機(jī)構(gòu)“機(jī)器之心”的原創(chuàng)譯文,微信公眾號(hào)“機(jī)器之心( id: almosthuman2014)”】

戳這里,看該作者更多好文

責(zé)任編輯:趙寧寧 來(lái)源: 51CTO專(zhuān)欄
相關(guān)推薦

2024-04-01 11:52:46

2018-03-25 09:11:31

大數(shù)據(jù)機(jī)器學(xué)習(xí)分析軟件

2021-10-13 17:58:57

模型人工智能函數(shù)

2013-03-20 15:49:28

大數(shù)據(jù)

2024-10-28 12:57:36

Pandas數(shù)據(jù)清洗

2021-06-11 13:30:28

神經(jīng)網(wǎng)絡(luò)機(jī)器學(xué)習(xí)

2013-03-20 16:23:53

數(shù)據(jù)清洗

2020-07-10 09:49:53

數(shù)據(jù)清理數(shù)據(jù)分析查找異常

2018-08-02 15:40:59

2011-05-04 10:07:47

堵塞噴頭清洗技巧

2025-03-21 08:20:00

數(shù)據(jù)清洗Python編程

2024-11-20 07:00:00

代碼數(shù)據(jù)清洗Python

2018-02-28 07:33:59

云遷移數(shù)據(jù)中心云計(jì)算

2018-03-01 10:48:52

云 數(shù)據(jù)中心

2024-01-03 18:53:13

語(yǔ)言模型LLM

2018-04-02 11:22:31

大數(shù)據(jù)Hadoop數(shù)據(jù)處理

2021-08-25 07:47:53

Pandas函數(shù)數(shù)據(jù)處理

2020-11-02 15:49:35

機(jī)器學(xué)習(xí)技術(shù)云計(jì)算

2016-08-23 00:39:25

2019-11-08 09:46:34

技術(shù)功能開(kāi)發(fā)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)