做者:東哥起飛,數據愛好者
Python數據科學
hello,你們好我是東哥!html
用Python處理數據你們都不陌生了,屬常規操做,但常規之下仍是也有些暗藏技巧的,本篇東哥分享6個好玩高效的操做,幫助你們提升效率。前端
Pandas Profiling
提供數據的一個總體報告,是一個幫助咱們理解數據的過程。它能夠簡單快速地對Pandas
的數據框數據進行探索性數據分析。python
其實,Pandas
中df.describe()
和df.info()
函數也能夠實現數據探索過程第一步。但它們只提供了對數據很是基本的概述。而Pandas
中的Profiling
功能簡單經過一行代碼就能顯示大量信息,同時還能生成交互式HTML
報告。微信
對於給定的數據集,Pandas中的profiling
包計算了如下統計信息:數據結構
由Pandas Profiling
包計算出的統計信息包括直方圖、衆數、相關係數、分位數、描述統計量、其餘信息包括類型、單一變量值、缺失值等。函數
安裝工具
用pip
和conda
便可,使用方法很簡單,以下:測試
pip install pandas-profiling conda install -c anaconda pandas-profiling
用法spa
以titanic數據集來演示profiling
的功能。debug
import pandas as pd import pandas_profiling df = pd.read_csv('titanic/train.csv') pandas_profiling.ProfileReport(df)
除了導入庫以外只須要一行代碼,就能顯示數據報告的詳細信息,包括必要的圖表。
還可使用如下代碼將報告導出到交互式HTML
文件中。
profile = pandas_profiling.ProfileReport(df) profile.to_file(outputfile="Titanic data profiling.html")
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} }
# 正常的print print(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 pprint pprint.pprint(my_dict) # 輸出結果以下: {'Student_ID': 34, 'Student_class': 5, 'Student_marks': {'English': 88, 'maths': 92, 'science': 95, 'social_science': 65}, 'Student_name': 'Tom'}
能夠清楚看到pprint
的優點之處,數據結構一目瞭然啊。
交互式調試器也是一個神奇的函數,若是在運行代碼單元格時出現報錯,能夠在新行中鍵入%debug
運行它。這將打開一個交互式調試環境,自動轉到報錯發生的位置,而且還能夠檢查程序中分配的變量值並執行操做。要退出調試器,按q
。好比下面這個例子。
x = [1,2,3] y = 2 z = 5 result = y+z print(result) result2 = x+y print(result2)
你們應該能看出x+y
確定會報錯,由於兩者不是一個類型,沒法進行運算操做。而後咱們敲入%debug
。
%debug
這時會出現對話框讓咱們互交式輸入命令,好比咱們能夠像下面這樣作。
這個東哥在以前也介紹過,對於數據探索的可視化分析超級好用,低代碼量即可生成漂亮的可視化圖形。下面舉一個例子,詳細的可參見這篇Python一行代碼搞定炫酷可視化,你須要瞭解一下Cufflinks。
cufflinks
在plotly
的基礎上作了一進一步的包裝,方法統一,參數配置簡單。其次它還能夠結合pandas
的dataframe
隨意靈活地畫圖。能夠把它形容爲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)
這是一個能讓你偷懶的import
神器,能夠提早在配置文件裏寫好要導入的三方庫,這樣每次編輯腳本的時候就省去了開頭的一大堆import 各類庫
,對於有經常使用和固定使用庫的朋友來講無疑也是提升效率的工具之一。
pyforest
支持大部分流行的數據科學庫,好比pandas
,numpy
,matplotlib
,seaborn
,sklearn
,tensorflow
等等,以及經常使用的輔助庫如os
,sys
,re
,pickle
等。
此用法對於本身頻繁調試很方便,但對於那些頻繁跨環境好比和其它人共享腳本調試的時候就不是很好用了,由於別人不必定使用它。
此庫東哥在以前也詳細介紹過 牛逼!這個Python庫居然能夠偷懶,和import說再見!看下面這個操做就明白了。
此方法僅適用於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了。看下面這個例子。
以上就是本次分享內容,歡迎各位朋友點贊留言收藏。
歡迎你們關注個人原創微信公衆號 Python數據科學。