pandas數(shù)據(jù)集的端到端處理
1. 數(shù)據(jù)集基本信息
df = pd.read_csv()
df.head():前五行;
df.info():
- rangeindex:行索引;
- data columns:列索引;
- dtypes:各個(gè)列的類(lèi)型,
- 主體部分是各個(gè)列值的情況,比如可判斷是否存在 NaN 值;
對(duì)于非數(shù)值型的屬性列
- df[‘some_categorical_columns'].value_counts():取值分布;
df.describe(): 各個(gè)列的基本統(tǒng)計(jì)信息
- count
- mean
- std
- min/max
- 25%, 50%, 75%:分位數(shù)
df.hist(bins=50, figsize=(20, 15)):統(tǒng)計(jì)直方圖;
對(duì) df 的每一列進(jìn)行展示:
train_prices = pd.DataFrame({'price': train_df.SalePrice,
'log(price+1)': np.log1p(train_df.SalePrice)})
# train_prices 共兩列,一列列名為 price,一列列名為 log(price+1)
train_prices.hist()
2. 數(shù)據(jù)集拆分
def split_train_test(data, test_ratio=.3): shuffled_indices = np.random.permutation(len(data)) test_size = int(len(data)*test_ratio) test_indices = shuffled_indices[:test_size] train_indices = shuffled_indices[test_size:] return data.iloc[train_indices], data.iloc[test_indices]
3. 數(shù)據(jù)預(yù)處理
- 一鍵把 categorical 型特征(字符串類(lèi)型)轉(zhuǎn)化為數(shù)值型:
>> df['label'] = pd.Categorical(df['label']).codes
- 一鍵把 categorical 型特征(字符串類(lèi)型)轉(zhuǎn)化為 one-hot 編碼:
>> df = pd.get_dummies(df)
- null 值統(tǒng)計(jì)與填充:
>> df.isnull().sum().sort_values(ascending=False).head() # 填充為 mean 值 >> mean_cols = df.mean() >> df = df.fillna(mean_cols) >> df.isnull().sum().sum() 0
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
Sphinx環(huán)境配置及VScode編寫(xiě)Rst文檔轉(zhuǎn)html的步驟
sphinx主要用于編寫(xiě) reStructuredText 和 Markdown 格式技術(shù)文檔,編寫(xiě)此類(lèi)技術(shù)文檔時(shí)Sphinx工具可將其轉(zhuǎn)為html、pdf、ePub等格式,這篇文章主要介紹了Sphinx環(huán)境配置及VScode編寫(xiě)Rst文檔轉(zhuǎn)html,需要的朋友可以參考下2023-03-03
解決python ogr shp字段寫(xiě)入中文亂碼的問(wèn)題
今天小編就為大家分享一篇解決python ogr shp字段寫(xiě)入中文亂碼的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
淺析python打包工具distutils、setuptools
python包在開(kāi)發(fā)中十分常見(jiàn),一般的使用套路是所有的功能做一個(gè)python模塊包,打包模塊,然后發(fā)布,安裝使用。這篇文章給大家介紹了python打包工具distutils、setuptools的相關(guān)知識(shí),感興趣的朋友一起看看吧2018-04-04
pandas之分組groupby()的使用整理與總結(jié)
這篇文章主要介紹了pandas之分組groupby()的使用整理與總結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
python3實(shí)現(xiàn)短網(wǎng)址和數(shù)字相互轉(zhuǎn)換的方法
這篇文章主要介紹了python3實(shí)現(xiàn)短網(wǎng)址和數(shù)字相互轉(zhuǎn)換的方法,涉及Python操作字符串的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
python讀寫(xiě)ini配置文件方法實(shí)例分析
這篇文章主要介紹了python讀寫(xiě)ini配置文件方法,實(shí)例分析了Python針對(duì)ini配置文件的相關(guān)讀寫(xiě)技巧,需要的朋友可以參考下2015-06-06
python 解決flask 圖片在線瀏覽或者直接下載的問(wèn)題
今天小編就為大家分享一篇python 解決flask 圖片在線瀏覽或者直接下載的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01

