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

對pandas進(jìn)行數(shù)據(jù)預(yù)處理的實例講解

 更新時間:2018年04月20日 16:22:56   作者:bigbao_num  
下面小編就為大家分享一篇對pandas進(jìn)行數(shù)據(jù)預(yù)處理的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

參加kaggle數(shù)據(jù)挖掘比賽,就第一個賽題Titanic的數(shù)據(jù),學(xué)習(xí)相關(guān)數(shù)據(jù)預(yù)處理以及模型建立,本博客關(guān)注基于pandas進(jìn)行數(shù)據(jù)預(yù)處理過程。包括數(shù)據(jù)統(tǒng)計、數(shù)據(jù)離散化、數(shù)據(jù)關(guān)聯(lián)性分析

引入包和加載數(shù)據(jù)

import pandas as pd
import numpy as np
train_df =pd.read_csv('../datas/train.csv') # train set
test_df = pd.read_csv('../datas/test.csv') # test set
combine = [train_df, test_df]

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

查看數(shù)據(jù)維度以及類型

缺失值處理

查看object數(shù)據(jù)統(tǒng)計信息

數(shù)值屬性離散化

計算特征與target屬性之間關(guān)系

查看數(shù)據(jù)維度以及類型

#查看前五條數(shù)據(jù)
print train_df.head(5) 
#查看每列數(shù)據(jù)類型以及nan情況
print train_df.info() 
# 獲得所有object屬性
print train_data.describe(include=['O']).columns 

查看object數(shù)據(jù)統(tǒng)計信息

#查看連續(xù)數(shù)值屬性基本統(tǒng)計情況
print train_df.describe() 
#查看object屬性數(shù)據(jù)統(tǒng)計情況
print train_df.describe(include=['O']) 
# 統(tǒng)計Title單列各個元素對應(yīng)的個數(shù)
print train_df['Title'].value_counts() 
# 屬性列刪除
train_df = train_df.drop(['Name', 'PassengerId'], axis=1) 

缺失值處理

# 直接丟棄缺失數(shù)據(jù)列的行
print df4.dropna(axis=0,subset=['col1']) # 丟棄nan的行,subset指定查看哪幾列 
print df4.dropna(axis=1) # 丟棄nan的列
# 采用其他值填充
dataset['Cabin'] = dataset['Cabin'].fillna('U') 
dataset['Title'] = dataset['Title'].fillna(0) 
# 采用出現(xiàn)最頻繁的值填充
freq_port = train_df.Embarked.dropna().mode()[0]
dataset['Embarked'] = dataset['Embarked'].fillna(freq_port)
# 采用中位數(shù)或者平均數(shù)填充
test_df['Fare'].fillna(test_df['Fare'].dropna().median(), inplace=True)
test_df['Fare'].fillna(test_df['Fare'].dropna().mean(), inplace=True)

數(shù)值屬性離散化,object屬性數(shù)值化

# 創(chuàng)造一個新列,F(xiàn)areBand,將連續(xù)屬性Fare切分成四份
train_df['FareBand'] = pd.qcut(train_df['Fare'], 4)
# 查看切分后的屬性與target屬性Survive的關(guān)系
train_df[['FareBand', 'Survived']].groupby(['FareBand'], as_index=False).mean().sort_values(by='FareBand', ascending=True)
# 建立object屬性映射字典 
title_mapping = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Royalty":5, "Officer": 6}
dataset['Title'] = dataset['Title'].map(title_mapping)

計算特征與target屬性之間關(guān)系

object與連續(xù)target屬性之間,可以groupby均值

object與離散target屬性之間,先將target數(shù)值化,然后groupby均值,或者分別條形統(tǒng)計圖

連續(xù)屬性需要先切割然后再進(jìn)行g(shù)roupby計算,或者pearson相關(guān)系數(shù)

print train_df[['AgeBand', 'Survived']].groupby(['AgeBand'], as_index=False).mean().sort_values(by='AgeBand', ascending=True)

總結(jié)pandas基本操作

”' 
創(chuàng)建df對象 
””' 
s1 = pd.Series([1,2,3,np.nan,4,5]) 
s2 = pd.Series([np.nan,1,2,3,4,5]) 
print s1 
dates = pd.date_range(“20130101”,periods=6) 
print dates 
df = pd.DataFrame(np.random.rand(6,4),index=dates,columns=list(“ABCD”)) 
# print df 
df2 = pd.DataFrame({“A”:1, 
‘B':pd.Timestamp(‘20130102'), 
‘C':pd.Series(1,index=list(range(4)),dtype='float32'), 
‘D':np.array([3]*4,dtype=np.int32), 
‘E':pd.Categorical([‘test','train','test','train']), 
‘F':'foo' 
}) 
# print df2.dtypes
df3 = pd.DataFrame({'col1':s1,
     'col2':s2
})
print df3

'''
2.查看df數(shù)據(jù)
'''
print df3.head(2) #查看頭幾條
print df3.tail(3) #查看尾幾條
print df.index #查看索引
print df.info() #查看非non數(shù)據(jù)條數(shù)
print type(df.values) #返回二元數(shù)組
# print df3.values
print df.describe() #對每列數(shù)據(jù)進(jìn)行初步的統(tǒng)計
print df3
print df3.sort_values(by=['col1'],axis=0,ascending=True) #按照哪幾列排序

'''
3.選擇數(shù)據(jù)
'''
ser_1 = df3['col1']
print type(ser_1) #pandas.core.series.Series
print df3[0:2] #前三行
print df3.loc[df3.index[0]] #通過index來訪問
print df3.loc[df3.index[0],['col2']] #通過行index,和列名來唯一確定一個位置
print df3.iloc[1] #通過位置來訪問
print df3.iloc[[1,2],1:2] #通過位置來訪問
print "==="
print df3.loc[:,['col1','col2']].as_matrix() # 返回nunpy二元數(shù)組
print type(df3.loc[:,['col1','col2']].as_matrix())

'''
4.布爾索引,過濾數(shù)據(jù)
'''
print df3[df3.col1 >2]
df4 = df3.copy()
df4['col3']=pd.Series(['one','two','two','three','one','two'])
print df4
print df4[df4['col3'].isin(['one','two'])]
df4.loc[:,'col3']="five"
print df4

'''
5.缺失值處理,pandas將缺失值用nan代替
'''
print pd.isnull(df4)
print df4.dropna(axis=0,subset=['col1']) # 丟棄nan的行,subset指定查看哪幾列
print df4.dropna(axis=1) # 丟棄nan的列

以上這篇對pandas進(jìn)行數(shù)據(jù)預(yù)處理的實例講解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python + OpenCV 實現(xiàn)LBP特征提取的示例代碼

    Python + OpenCV 實現(xiàn)LBP特征提取的示例代碼

    這篇文章主要介紹了Python + OpenCV 實現(xiàn)LBP特征提取的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • Python內(nèi)置函數(shù)詳細(xì)解析

    Python內(nèi)置函數(shù)詳細(xì)解析

    這篇文章主要介紹了Python內(nèi)置函數(shù)詳細(xì)解析,Python?自帶了很多的內(nèi)置函數(shù),極大地方便了我們的開發(fā),下文小編總結(jié)了一些內(nèi)置函數(shù)的相關(guān)內(nèi)容,需要的小伙伴可以參考一下
    2022-05-05
  • Pytest命令行選項的具體使用

    Pytest命令行選項的具體使用

    pytest是一個流行的Python測試框架,它提供了許多命令行選項,本文主要介紹了Pytest命令行選項的具體使用,具有一定的參考價值,感興趣的可以了解一下
    2023-11-11
  • Python中函數(shù)調(diào)用9大方法小結(jié)

    Python中函數(shù)調(diào)用9大方法小結(jié)

    在Python中,函數(shù)是一種非常重要的編程概念,它們使得代碼模塊化、可重用,并且能夠提高代碼的可讀性,本文將深入探討Python函數(shù)調(diào)用的9種方法,需要的可以參考下
    2024-01-01
  • python tkinter canvas 顯示圖片的示例

    python tkinter canvas 顯示圖片的示例

    今天小編就為大家分享一篇python tkinter canvas 顯示圖片的示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06
  • Python爬蟲requests庫多種用法實例

    Python爬蟲requests庫多種用法實例

    這篇文章主要介紹了Python爬蟲requests庫多種用法實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-05-05
  • Python爬取百度地圖POI數(shù)據(jù)代碼的步驟

    Python爬取百度地圖POI數(shù)據(jù)代碼的步驟

    爬取百度地圖的POI數(shù)據(jù)涉及法律和道德問題,因為這類數(shù)據(jù)受到版權(quán)保護(hù),且大多數(shù)在線地圖服務(wù)都有嚴(yán)格的反爬蟲措施,這篇文章主要介紹了Python爬取百度地圖POI數(shù)據(jù)代碼,需要的朋友可以參考下
    2024-08-08
  • Python實現(xiàn)文件只讀屬性的設(shè)置與取消

    Python實現(xiàn)文件只讀屬性的設(shè)置與取消

    這篇文章主要為大家詳細(xì)介紹了Python如何實現(xiàn)設(shè)置文件只讀與取消文件只讀的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2023-07-07
  • django ObjectDoesNotExist 和 DoesNotExist的用法

    django ObjectDoesNotExist 和 DoesNotExist的用法

    這篇文章主要介紹了django ObjectDoesNotExist 和 DoesNotExist的用法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • TensorFlow實現(xiàn)非線性支持向量機的實現(xiàn)方法

    TensorFlow實現(xiàn)非線性支持向量機的實現(xiàn)方法

    本篇文章主要介紹了TensorFlow實現(xiàn)非線性支持向量機的實現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04

最新評論