用Python處理數(shù)據(jù)大家都不陌生了,屬于常規(guī)操作,但常規(guī)之下也還是暗藏技巧的,本篇就來分享6個(gè)好玩高效的操作,幫助大家提高效率。
一、Pandas Profiling
Pandas Profiling
提供數(shù)據(jù)的一個(gè)整體報(bào)告,是一個(gè)幫助我們理解數(shù)據(jù)的過程。它可以簡(jiǎn)單快速地對(duì)Pandas
的數(shù)據(jù)框數(shù)據(jù)進(jìn)行探索性數(shù)據(jù)分析。
其實(shí),Pandas
中df.describe()
和df.info()
函數(shù)也可以實(shí)現(xiàn)數(shù)據(jù)探索過程第一步。但它們只提供了對(duì)數(shù)據(jù)非?;镜母攀?。而Pandas
中的Profiling
功能簡(jiǎn)單通過一行代碼就能顯示大量信息,同時(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 pdimport pandas_profilingdf = pd.read_csv('titanic/train.csv')pandas_profiling.ProfileReport(df)
用法
以titanic數(shù)據(jù)集來演示profiling
的功能。
import pandas as pdimport pandas_profilingdf = 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} }
# 正常的printprint(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 pprintpprint.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 = 2z = 5result = y+zprint(result)result2 = x+yprint(result2)
大家應(yīng)該能看出x+y
肯定會(huì)報(bào)錯(cuò),因?yàn)槎卟皇且粋€(gè)類型,無法進(jìn)行運(yùn)算操作。然后我們敲入%debug
。
%debug
這時(shí)會(huì)出現(xiàn)對(duì)話框讓我們互交式輸入命令,比如我們可以像下面這樣做。
四、Cufflinks
這個(gè)庫(kù)之前也介紹過,對(duì)于數(shù)據(jù)探索的可視化分析超級(jí)好用,低代碼量便可生成漂亮的可視化圖形。下面舉一個(gè)例子:
cufflinks
在plotly
的基礎(chǔ)上做了一進(jìn)一步的包裝,方法統(tǒng)一,參數(shù)配置簡(jiǎn)單。其次它還可以結(jié)合pandas
的dataframe
隨意靈活地畫圖。可以把它形容為"pandas like visualization"。
比如下面的lins線圖
。
import pandas as pdimport cufflinks as cfimport numpy as npcf.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)圖就愛不釋手啊,有木有!
五、Pyforest
這是一個(gè)能讓你偷懶的import
神器,可以提前在配置文件里寫好要導(dǎo)入的三方庫(kù),這樣每次編輯腳本的時(shí)候就省去了開頭的一大堆import 各種庫(kù)
,對(duì)于有常用和固定使用庫(kù)的朋友來說無疑也是提高效率的工具之一。
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人不一定使用它。
看下面這個(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è)例子。
以上就是6個(gè)冷門但卻實(shí)用的數(shù)據(jù)分析庫(kù)。
如果文章對(duì)你有幫助,歡迎轉(zhuǎn)發(fā)/點(diǎn)贊/收藏~