用Python處理數據大家都不陌生了,屬于常規操作,但常規之下也還是暗藏技巧的,本篇就來分享6個好玩高效的操作,幫助大家提高效率。
一、Pandas Profiling
Pandas Profiling
提供數據的一個整體報告,是一個幫助我們理解數據的過程。它可以簡單快速地對Pandas
的數據框數據進行探索性數據分析。
其實,Pandas
中df.describe()
和df.info()
函數也可以實現數據探索過程第一步。但它們只提供了對數據非常基本的概述。而Pandas
中的Profiling
功能簡單通過一行代碼就能顯示大量信息,同時還能生成交互式HTML
報告。
對于給定的數據集,Pandas中的profiling
包計算了以下統計信息:
由Pandas Profiling
包計算出的統計信息包括直方圖、眾數、相關系數、分位數、描述統計量、其他信息包括類型、單一變量值、缺失值等。
安裝
用pip
和conda
即可,使用方法很簡單,如下:
import pandas as pdimport pandas_profilingdf = pd.read_csv('titanic/train.csv')pandas_profiling.ProfileReport(df)
用法
以titanic數據集來演示profiling
的功能。
import pandas as pdimport pandas_profilingdf = pd.read_csv('titanic/train.csv')pandas_profiling.ProfileReport(df)
除了導入庫之外只需要一行代碼,就能顯示數據報告的詳細信息,包括必要的圖表。
還可以使用以下代碼將報告導出到交互式HTML
文件中。
profile = pandas_profiling.ProfileReport(df)profile.to_file(outputfile="Titanic data profiling.html")
二、pretty print
pprint
是Python中的內置模塊。它能夠以格式清晰,可讀性強漂亮
格式打印任意數據結構。一個例子對比下print
和pprint
。
# 定義個字典,測試用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)# 輸出結果如下:{'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)# 輸出結果如下:{'Student_ID': 34, 'Student_class': 5, 'Student_marks': {'English': 88, 'maths': 92, 'science': 95, 'social_science': 65}, 'Student_name': 'Tom'}
可以清楚看到pprint
的優勢之處,數據結構一目了然啊。
三、Python Debugger
交互式調試器也是一個神奇的函數,如果在運行代碼單元格時出現報錯,可以在新行中鍵入%debug
運行它。這將打開一個交互式調試環境,自動轉到報錯發生的位置,并且還可以檢查程序中分配的變量值并執行操作。要退出調試器,按q
。比如下面這個例子。
x = [1,2,3]y = 2z = 5result = y+zprint(result)result2 = x+yprint(result2)
大家應該能看出x+y
肯定會報錯,因為二者不是一個類型,無法進行運算操作。然后我們敲入%debug
。
%debug
這時會出現對話框讓我們互交式輸入命令,比如我們可以像下面這樣做。
四、Cufflinks
這個庫之前也介紹過,對于數據探索的可視化分析超級好用,低代碼量便可生成漂亮的可視化圖形。下面舉一個例子:
cufflinks
在plotly
的基礎上做了一進一步的包裝,方法統一,參數配置簡單。其次它還可以結合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)
看著這動態圖就愛不釋手啊,有木有!
五、Pyforest
這是一個能讓你偷懶的import
神器,可以提前在配置文件里寫好要導入的三方庫,這樣每次編輯腳本的時候就省去了開頭的一大堆import 各種庫
,對于有常用和固定使用庫的朋友來說無疑也是提高效率的工具之一。
pyforest
支持大部分流行的數據科學庫,比如pandas
,numpy
,matplotlib
,seaborn
,sklearn
,tensorflow
等等,以及常用的輔助庫如os
,sys
,re
,pickle
等。
此用法對于自己頻繁調試很方便,但對于那些頻繁跨環境比如和其它人共享腳本調試的時候就不是很好用了,因為別人不一定使用它。
看下面這個操作就明白了:
六、Jupyter notebook的筆記高亮
此方法僅適用于Jupyter notebook
中,當我們想高亮筆記,讓筆記變得美觀的時候,這個方法非常的香。
筆記的高亮的顏色根據不同情況分為幾種,前端的同學一看就明白,區別就是每種顏色代碼的class
類型不一樣,其它只要在div
標簽中寫內容就好。下面看下用法。
藍色代表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>
這里有個小提示:如果你直接復制到jupyter notebook
中可能會報錯,因為默認是代碼的格式,所以你需要選中單元格按Esc
變成可切換模式,然后再按Y
切換成文本模式。這時候再運行shift+ok
就ok了??聪旅孢@個例子。
以上就是6個冷門但卻實用的數據分析庫。
如果文章對你有幫助,歡迎轉發/點贊/收藏~