# 只用一行程式碼分析數據！？實用的 Python Package pandas\_profiling

台股研究 2020年7月21日 約 2 分鐘閱讀

只要 df.profile\_report() 一行，pandas\_profiling 就能自動生成本益比、殖利率、股價淨值比的分佈圖、缺漏率與欄位相關性熱力圖。文中用台股 BWIBBU 爬蟲示範，幫你檢查殖利率 20% 為 0、本益比缺漏背後是否合理，加速資料前處理。

這篇文章介紹一個好用的 package，只要一行，就可以做出精美、詳細的圖表分析 還可以幫你查看資料是否有缺漏和錯誤的情況！

[![pandas\_profiling 一行程式碼分析數據封面圖](https://r2.finlab.finance/blog/one-line-info-dataframe/thumbnail-6-1024x683.jpeg)](https://r2.finlab.finance/blog/one-line-info-dataframe/thumbnail-6-1024x683.jpeg)

依本文重跑

## 執行這個策略

複製提示詞，讓 AI 依照上方證據的資料、條件與風險檢查重跑一次。

告訴你的AI：

幫我設定 FinLab，重現這篇文章，請讀：https\://finlab.finance/setup?relatedUrl=/blog/pandas-profiling-one-line-data-analysis

[免安裝，開啟 FinLab Studio](https://studio.finlab.finance/chat?lang=zh\&research_context=%7B%22v%22%3A1%2C%22source_url%22%3A%22https%3A%2F%2Ffinlab.finance%2Fblog%2Fpandas-profiling-one-line-data-analysis%22%7D)

今天的程式碼成果：

![pandas\_profiling profile\_report 產生互動式資料分析報告動畫示範](https://r2.finlab.finance/blog/one-line-info-dataframe/profile_df.gif)

## 先取得要分析之資料

今天我們來分析一下股票的本益比、股價淨值比和殖利率， 先提供給大家一個爬蟲，讓大家可以直接把資料爬下來：

顯示程式碼

```
import datetime
import pandas as pd
import warnings
import requests
from io import StringIO
import pandas_profiling
 
def crawler(date):
    datestr = date.strftime('%Y%m%d')
    url = 'https://www.twse.com.tw/exchangeReport/BWIBBU_d?response=csv&date='+datestr+'&selectType=ALL'
    res = requests.get(url)
    df = pd.read_csv(StringIO(res.text), header=1)
    df['本益比'] = pd.to_numeric(df['本益比'], errors='coerce')
    
    return df.dropna(thresh=3).dropna(thresh=0.8, axis=1)
```

接下來我們就呼叫 `crawler` 這個函式，就可以將財務數據資料都爬取下來囉！

顯示程式碼

```
df = crawler(datetime.date(2019,10,7))
df.head()
```

[![爬取台股本益比殖利率股價淨值比資料 df.head() 結果表格](https://r2.finlab.finance/blog/one-line-info-dataframe/head-1024x190.png)](https://r2.finlab.finance/blog/one-line-info-dataframe/head-1024x190.png)

### 1. 用舊的方法來分析資料

最簡單的方式，假如有用過 `pandas` 的大家應該都知道， 可以使用 `df.describe()` 來大致上觀察數據的樣貌 假如你對 pandas 不太熟悉，它有點像是拿來處理表格的資料， 就像是 python 界的 excel 一樣，雖然它非常好用，但是有點太古老（無聊）了！

顯示程式碼

```
df.describe()
```

[![使用 pandas df.describe() 觀察股票數據統計樣貌](https://r2.finlab.finance/blog/one-line-info-dataframe/describe-2-1024x262.png)](https://r2.finlab.finance/blog/one-line-info-dataframe/describe-2-1024x262.png)

### 2. 酷炫的方法資料分析

首先我們可以安裝 pandas\_profiling

顯示程式碼

```
pip install pandas_profiling
```

接下來就可以直接來使用：

顯示程式碼

```
import pandas_profiling
df = crawler(datetime.date(2019,10,7))
df.profile_report()
```

然後我們就會看到超精美的圖表！

#### 找出資料的缺漏或問題

我覺得 `pandas_profiling` 很棒的地方在於， 可以將資料一口氣全部統整給我們，方便我們去做資料前處理， 例如下圖中的左下角，就可以看到每一個column有哪些警告，

[![pandas\_profiling 資料總覽報告顯示每個欄位的警告與缺漏](https://r2.finlab.finance/blog/one-line-info-dataframe/profile-1024x562.png)](https://r2.finlab.finance/blog/one-line-info-dataframe/profile-1024x562.png)

* 本益比有 20.4 % 是缺漏的：這是正常情況，因為本益比小於零不顯示
* 殖利率有 20.0 % 是 0：這也是正常情況，因為公司不一定有發股利股息
* 財報都是用第 2 季的財報：這也是正常的！

這些檢查可以幫助我們判斷資料是否可靠。 除了上述的檢驗外，我們還可以

#### 輕鬆檢視每一個 column 的資料

這邊可以看到每一種資料的分佈，可以讓我們更好的掌握數據分佈型態

[![pandas\_profiling 顯示每個欄位資料分佈的直方圖](https://r2.finlab.finance/blog/one-line-info-dataframe/hist-1-1024x467.png)](https://r2.finlab.finance/blog/one-line-info-dataframe/hist-1-1024x467.png)

#### 檢視資料相關性

這邊還可以檢視資料的相關性，這邊有些複雜的名詞，哪天再來跟各位介紹

[![pandas\_profiling 檢視資料欄位之間相關性的熱力圖](https://r2.finlab.finance/blog/one-line-info-dataframe/corr-1024x471.png)](https://r2.finlab.finance/blog/one-line-info-dataframe/corr-1024x471.png)

`pandas_profiling` 真的是很不錯的 package， 可以幫我們進一步的認識資料， 假如你也覺得這個 package 很實用， 也歡迎你分享給大家喔！

***

## 延伸閱讀

* [1 分鐘學會！用 Lux API 自動視覺化 Pandas 資料](https://finlab.finance/blog/lux-api-pandas-auto-visualization)
* [利用 Pandas 輕鬆選股：Python 實作教學](https://finlab.finance/blog/pandas-stock-screening-python-tutorial)
* [選股四大面向總覽](https://finlab.finance/tools/stock-selection)

**投資警語：本文僅供教學參考，不構成投資建議。過去績效不代表未來表現，投資有風險。**

![韓承佑](https://finlab.finance/people/cheng-yu-han-avatar.jpg)

韓承佑

創辦人 & 量化研究員

韓承佑是 FinLab 創辦人，也是開源 Python 量化套件 finlab 的作者，長年投入台股與美股的量化策略研究、回測與教學。

[在 GitHub 上追蹤 →](https://github.com/koreal6803) [在 LinkedIn 上連結 →](https://www.linkedin.com/in/cheng-yu-han/)

FinLab AI

### 想建立自己的策略？

用自然語言描述你的選股想法，AI 自動驗證、回測、給你答案

[免費開始](https://finlab.finance/#get-started)

## 更多價值與基本面研究

[查看全部](https://finlab.finance/blog/category/value)

* [![現金股利調高選股：加進月營收動能，7 年年化 59.9%、夏普 1.83](https://r2.finlab.finance/blog/cash-dividend-increase-stock-selection/chart_equity.png) 現金股利調高選股：加進月營收動能，7 年年化 59.9%、夏普 1.83 年化報酬 59.9% · 台股](https://finlab.finance/blog/cash-dividend-increase-stock-selection)
* [![營收創新高選股：月營收動能優先買營收創 12 個月新高，年化 52.4%、夏普 1.62](https://r2.finlab.finance/blog/revenue-new-high-stock-selection/chart_equity.png) 營收創新高選股：月營收動能優先買營收創 12 個月新高，年化 52.4%、夏普 1.62 年化報酬 52.4% · 台股](https://finlab.finance/blog/revenue-new-high-stock-selection)
* [![月營收動能選股：台股 11 年回測年化 50%，夏普 1.46（附程式碼）](https://r2.finlab.finance/blog/monthly-revenue-momentum/chart_summary.png) 月營收動能選股：台股 11 年回測年化 50%，夏普 1.46（附程式碼） 年化報酬 50.0% · 台股](https://finlab.finance/blog/monthly-revenue-momentum)
* [![財報選股 3 指標回測：2020 年起年化 45.6%，近三年輸 0050](https://r2.finlab.finance/blog/fundamental-3-indicators/image-4-1024x938.png) 財報選股 3 指標回測：2020 年起年化 45.6%，近三年輸 0050 年化報酬 45.6% · 台股](https://finlab.finance/blog/fundamental-3-indicators)

來源：https://finlab.finance/blog/pandas-profiling-one-line-data-analysis｜資料日期：依上文各表格與段落標示｜頁面更新：2026-11-25
