太香了!推薦6個(gè)Python數(shù)據(jù)分析神器??!

出品:Python數(shù)據(jù)科學(xué)
用Python處理數(shù)據(jù)大家都不陌生了,屬常規(guī)操作,但常規(guī)之下還是也有些暗藏技巧的,本篇東哥分享6個(gè)好玩高效的操作,幫助大家提高效率。
一、Pandas Profiling
Pandas Profiling提供數(shù)據(jù)的一個(gè)整體報(bào)告,是一個(gè)幫助我們理解數(shù)據(jù)的過(guò)程。它可以簡(jiǎn)單快速地對(duì)Pandas的數(shù)據(jù)框數(shù)據(jù)進(jìn)行探索性數(shù)據(jù)分析。
其實(shí),Pandas中df.describe()和df.info()函數(shù)也可以實(shí)現(xiàn)數(shù)據(jù)探索過(guò)程第一步。但它們只提供了對(duì)數(shù)據(jù)非常基本的概述。而Pandas中的Profiling功能簡(jiǎn)單通過(guò)一行代碼就能顯示大量信息,同時(shí)還能生成交互式HTML報(bào)告。
對(duì)于給定的數(shù)據(jù)集,Pandas中的profiling包計(jì)算了以下統(tǒng)計(jì)信息:

由Pandas Profiling包計(jì)算出的統(tǒng)計(jì)信息包括直方圖、眾數(shù)、相關(guān)系數(shù)、分位數(shù)、描述統(tǒng)計(jì)量、其他信息包括類型、單一變量值、缺失值等。
安裝
用pip和conda即可,使用方法很簡(jiǎn)單,如下:
import?pandas?as?pd
import?pandas_profiling
df?=?pd.read_csv('titanic/train.csv')
pandas_profiling.ProfileReport(df)
用法
以titanic數(shù)據(jù)集來(lái)演示profiling的功能。
import?pandas?as?pd
import?pandas_profiling
df?=?pd.read_csv('titanic/train.csv')
pandas_profiling.ProfileReport(df)?

除了導(dǎo)入庫(kù)之外只需要一行代碼,就能顯示數(shù)據(jù)報(bào)告的詳細(xì)信息,包括必要的圖表。
還可以使用以下代碼將報(bào)告導(dǎo)出到交互式HTML文件中。
profile?=?pandas_profiling.ProfileReport(df)
profile.to_file(outputfile="Titanic?data?profiling.html")

二、pretty print
pprint是Python中的內(nèi)置模塊。它能夠以格式清晰,可讀性強(qiáng)漂亮格式打印任意數(shù)據(jù)結(jié)構(gòu)。一個(gè)例子對(duì)比下print和pprint。
#?定義個(gè)字典,測(cè)試用
my_dict?=?{'Student_ID':?34,'Student_name'?:?'Tom',?'Student_class'?:?5,
??????????'Student_marks'?:?{'maths'?:?92,
????????????????????????????'science'?:?95,
????????????????????????????'social_science'?:?65,
????????????????????????????'English'?:?88}
??????????}
#?正常的print
print(my_dict)
#?輸出結(jié)果如下:
{'Student_ID':?34,?'Student_name':?'Tom',?'Student_class':?5,?'Student_marks':?{'maths':?92,?'science':?95,?'social_science':?65,?'English':?88}}
pprint
#?使用pprint輸出
import?pprint
pprint.pprint(my_dict)
#?輸出結(jié)果如下:
{'Student_ID':?34,
?'Student_class':?5,
?'Student_marks':?{'English':?88,
???????????????????'maths':?92,
???????????????????'science':?95,
???????????????????'social_science':?65},
?'Student_name':?'Tom'}
可以清楚看到pprint的優(yōu)勢(shì)之處,數(shù)據(jù)結(jié)構(gòu)一目了然啊。
三、Python Debugger
交互式調(diào)試器也是一個(gè)神奇的函數(shù),如果在運(yùn)行代碼單元格時(shí)出現(xiàn)報(bào)錯(cuò),可以在新行中鍵入%debug運(yùn)行它。這將打開一個(gè)交互式調(diào)試環(huán)境,自動(dòng)轉(zhuǎn)到報(bào)錯(cuò)發(fā)生的位置,并且還可以檢查程序中分配的變量值并執(zhí)行操作。要退出調(diào)試器,按q。比如下面這個(gè)例子。
x?=?[1,2,3]
y?=?2
z?=?5
result?=?y+z
print(result)
result2?=?x+y
print(result2)
大家應(yīng)該能看出x+y肯定會(huì)報(bào)錯(cuò),因?yàn)槎卟皇且粋€(gè)類型,無(wú)法進(jìn)行運(yùn)算操作。然后我們敲入%debug。
%debug
這時(shí)會(huì)出現(xiàn)對(duì)話框讓我們互交式輸入命令,比如我們可以像下面這樣做。

四、Cufflinks
這個(gè)東哥在之前也介紹過(guò),對(duì)于數(shù)據(jù)探索的可視化分析超級(jí)好用,低代碼量便可生成漂亮的可視化圖形。下面舉一個(gè)例子,詳細(xì)的可參見(jiàn)這篇Python一行代碼搞定炫酷可視化,你需要了解一下Cufflinks。
cufflinks在plotly的基礎(chǔ)上做了一進(jìn)一步的包裝,方法統(tǒng)一,參數(shù)配置簡(jiǎn)單。其次它還可以結(jié)合pandas的dataframe隨意靈活地畫圖??梢园阉稳轂?/span>"pandas like visualization"。
比如下面的lins線圖。
import?pandas?as?pd
import?cufflinks?as?cf
import?numpy?as?np
cf.set_config_file(offline=True)
cf.datagen.lines(1,500).ta_plot(study='sma',periods=[13,21,55])

再比如box箱型圖。
cf.datagen.box(20).iplot(kind='box',legend=False)

看著這動(dòng)態(tài)圖就愛(ài)不釋手啊,有木有!
五、Pyforest
這是一個(gè)能讓你偷懶的import神器,可以提前在配置文件里寫好要導(dǎo)入的三方庫(kù),這樣每次編輯腳本的時(shí)候就省去了開頭的一大堆import 各種庫(kù),對(duì)于有常用和固定使用庫(kù)的朋友來(lái)說(shuō)無(wú)疑也是提高效率的工具之一。
pyforest支持大部分流行的數(shù)據(jù)科學(xué)庫(kù),比如pandas,numpy,matplotlib,seaborn,sklearn,tensorflow等等,以及常用的輔助庫(kù)如os,sys,re,pickle等。
此用法對(duì)于自己頻繁調(diào)試很方便,但對(duì)于那些頻繁跨環(huán)境比如和其它人共享腳本調(diào)試的時(shí)候就不是很好用了,因?yàn)閯e人不一定使用它。
此庫(kù)東哥在之前也詳細(xì)介紹過(guò)?牛逼!這個(gè)Python庫(kù)竟然可以偷懶,和import說(shuō)再見(jiàn)!看下面這個(gè)操作就明白了。

六、Jupyter notebook的筆記高亮
此方法僅適用于Jupyter notebook中,當(dāng)我們想高亮筆記,讓筆記變得美觀的時(shí)候,這個(gè)方法非常的香。
筆記的高亮的顏色根據(jù)不同情況分為幾種,前端的同學(xué)一看就明白,區(qū)別就是每種顏色代碼的class類型不一樣,其它只要在div標(biāo)簽中寫內(nèi)容就好。下面看下用法。
藍(lán)色代表info
<div?class="alert?alert-block?alert-info">
<b>Tip:b>?Use?blue?boxes?(alert-info)?for?tips?and?notes.?
If?it’s?a?note,?you?don’t?have?to?include?the?word?“Note”.
div>

黃色代表warning
<div?class="alert?alert-block?alert-warning">
<b>Example:b>?Yellow?Boxes?are?generally?used?to?include?additional?examples?or?mathematical?formulas.
div>

綠色代表success
<div?class="alert?alert-block?alert-success">
Use?green?box?only?when?necessary?like?to?display?links?to?related?content.
div>

紅色代表danger
<div?class="alert?alert-block?alert-danger">
It?is?good?to?avoid?red?boxes?but?can?be?used?to?alert?users?to?not?delete?some?important?part?of?code?etc.?
div>

這里有個(gè)小提示東哥提示下,如果你直接復(fù)制到jupyter notebook中可能會(huì)報(bào)錯(cuò),因?yàn)槟J(rèn)是代碼的格式,所以你需要選中單元格按Esc變成可切換模式,然后再按Y切換成文本模式。這時(shí)候再運(yùn)行shift+ok就ok了??聪旅孢@個(gè)例子。

歡迎老鐵們多多分享和點(diǎn)個(gè)在看!
