99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀Pandas數(shù)據(jù)挖掘與分析時的常用方法
Pandas數(shù)據(jù)挖掘與分析時的常用方法
2022-03-07
收藏
Pandas<a href='/map/shujuwajue/' style='color:#000;font-size:inherit;'>數(shù)據(jù)挖掘</a>與分析時的常用方法

作者:俊欣

來源:關(guān)于數(shù)據(jù)分析與可視化

今天我們來講一下用Pandas模塊對數(shù)據(jù)集進行分析的時候,一些經(jīng)常會用到的配置,通過這些配置的幫助,我們可以更加有效地來分析和挖掘出有價值的數(shù)據(jù)。

數(shù)據(jù)集的準(zhǔn)備

這次我們需要用到的數(shù)據(jù)集是廣為人所知的泰坦尼克號的乘客數(shù)據(jù),我們先導(dǎo)入并且讀取數(shù)據(jù)集

import pandas as pd 
df = pd.read_csv("train.csv")

展示更多的行

Pandas默認(rèn)只展示60行的數(shù)據(jù),如果數(shù)據(jù)集當(dāng)中的數(shù)量超過了60行,

pd.get_option('display.max_rows') ## 或者是 pd.options.display.max_rows

output

60 

要是數(shù)據(jù)集當(dāng)中的數(shù)據(jù)超過了60行,則會將中間的數(shù)據(jù)給折疊起來,展示出來前面的5行以及最后的5行,如下圖所示

Pandas<a href='/map/shujuwajue/' style='color:#000;font-size:inherit;'>數(shù)據(jù)挖掘</a>與分析時的常用方法

當(dāng)然我們也可以改變最多展示出來的行數(shù),代碼如下

pd.set_option('display.max_rows', 200)

或者我們要是想將所有的數(shù)據(jù)都給展示出來的話,就設(shè)置成None,當(dāng)然要是我們的數(shù)據(jù)集很長很長的話,有幾萬行幾十萬行的話,這么做可能會使得notebook崩掉

pd.set_option('display.max_rows', None)
或者是 # pd.options.display.max_rows = None 

展示更多的列

同樣地,pandas默認(rèn)只展示20列的數(shù)據(jù)

pd.get_option('display.max_columns') # pd.options.display.max_columns 

output

20 

要是數(shù)據(jù)集超過了20列的數(shù)據(jù),中間的幾列數(shù)據(jù)就會折疊起來,如下圖所示

Pandas<a href='/map/shujuwajue/' style='color:#000;font-size:inherit;'>數(shù)據(jù)挖掘</a>與分析時的常用方法

當(dāng)然我們也可以改變這個值,例如當(dāng)數(shù)據(jù)集當(dāng)中的數(shù)據(jù)超過了50列才會被折疊,代碼如下

# 當(dāng)數(shù)據(jù)集當(dāng)中的數(shù)據(jù)超過了50列才會被折疊 pd.set_option('display.max_columns', 50) # pd.options.display.max_columns = 50 

或者就干脆展示出來所有的列

pd.set_option('display.max_columns', None) # pd.options.display.max_columns = None 

改變列的寬度

當(dāng)我們想要展示數(shù)據(jù)集當(dāng)中的前5列的時候

df.head()

output

Pandas<a href='/map/shujuwajue/' style='color:#000;font-size:inherit;'>數(shù)據(jù)挖掘</a>與分析時的常用方法

我們發(fā)現(xiàn)“Name”這一列當(dāng)中的第二行因為字?jǐn)?shù)比較多,就用了省略號來代替,這是因為Pandas對顯示數(shù)據(jù)的量也是有限制的,

pd.get_option('display.max_colwidth') # pd.options.display.max_colwidth 

當(dāng)然我們也能改變這個默認(rèn)值,代碼如下

pd.set_option('display.max_colwidth', 500) # pd.options.display.max_colwidth = 500 

或者顯示出所有的內(nèi)容

pd.set_option('display.max_colwidth', None) # pd.options.display.max_colwidth = None 

改變浮點數(shù)的精度

或許你也察覺到了Pandas對于浮點數(shù)的精度的展示也是有限制的,如下圖所示

Pandas<a href='/map/shujuwajue/' style='color:#000;font-size:inherit;'>數(shù)據(jù)挖掘</a>與分析時的常用方法

默認(rèn)只展示小數(shù)點后面的6位小數(shù),

pd.get_option('display.precision') # pd.options.display.precision 

output

6 

要是我們只是希望展示小數(shù)點后面2位小數(shù),則可以這么來做

pd.set_option('display.precision', 2) # pd.options.display.precision = 2 

我們來看一下最終的效果如何

df.head()

output

Pandas<a href='/map/shujuwajue/' style='color:#000;font-size:inherit;'>數(shù)據(jù)挖掘</a>與分析時的常用方法

個性化展示數(shù)字

有時候我們遇到例如貨幣、百分比、小數(shù)等數(shù)字時,可以通過pandas當(dāng)中的display.float_format方法來個性化展示數(shù)字,

pd.set_option('display.float_format', '{:,.2f}'.format) df_test 

例如我們希望對數(shù)字添加百分號來展示,代碼如下

pd.set_option('display.float_format', '{:.2f}%'.format) df_test 

例如我們希望在數(shù)字面前添加貨幣符號,代碼如下

pd.set_option('display.float_format', '${:.2f}'.format) df_test 

改變圖表繪制的后端

默認(rèn)的Pandas模塊對圖表的繪制是以matplotlib為后端的,但是以此為后端繪制出來的圖表并不是動態(tài)可交互的,我們可以改成以plotly或者是altair為后端來繪制圖表,

import pandas as pd import numpy as np
pd.set_option('plotting.backend', 'altair') data = pd.Series(np.random.randn(100).cumsum()) data.plot()

重置回默認(rèn)的配置

除了上面小編介紹的配置之外,大家也可以自行對數(shù)據(jù)集的展示的配置進行調(diào)整,首先我們看一下總共有哪些配置可以供我們來調(diào)整

pd.describe_option()

output

Pandas<a href='/map/shujuwajue/' style='color:#000;font-size:inherit;'>數(shù)據(jù)挖掘</a>與分析時的常用方法

要是我們指定想要看橫軸方向上的配置,可以這么來做

pd.describe_option("rows")

output

Pandas<a href='/map/shujuwajue/' style='color:#000;font-size:inherit;'>數(shù)據(jù)挖掘</a>與分析時的常用方法

依次我們可以對最大展示出來的行數(shù)、最少展示出來的行數(shù)進行調(diào)整,而要是你想將所有的配置還原成默認(rèn)值,可以這么來做

pd.reset_option('all')

數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務(wù)器是否宕機 new_captcha: data.new_captcha, // 用于宕機時表示是新驗證碼的宕機 product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }