Pandas的describe()函數的返回值到底是什麼意思?

2024年2月6日 25点热度 0人点赞

提起Pandas,想必大傢都使用過describe()函數,那這個函數的8項輸出到底是什麼意思呢?

Pandas 中的 describe() 方法提供了 DataFrame 或 Series 數值列的統計摘要。這個方法默認隻對數值型列進行計算,但也可以應用於非數值型數據。

數值型

對於數值型數據(默認),describe() 方法通常返回以下統計信息:

  1. count: 非空(非 NA/null)值的數量。
  2. mean: 數值的平均值。
  3. std: 標準差,表示數值的離散程度。
  4. min: 最小值。
  5. 25% (第一四分位數): 小於這個值的數值占總數的 25%。
  6. 50% (中位數): 正中間的數值。當數值總數是奇數時,是中間的數值;當總數是偶數時,是中間兩個數值的平均數。
  7. 75% (第三四分位數): 小於這個值的數值占總數的 75%。
  8. max: 最大值。

示例代碼如下:

import pandas as pd
data = {'Col1': [1, 2, 3, 4, 5],
        'Col2': [6, 7, 8, 9, 10]}
df = pd.DataFrame(data)
            Col1       Col2
count   5.000000   5.000000
mean    3.000000   8.000000
std     1.581139   1.581139
min     1.000000   6.000000
25%     2.000000   7.000000
50%     3.000000   8.000000
75%     4.000000   9.000000
max     5.000000  10.000000

#pgc-card .pgc-card-href { text-decoration: none; outline: none; display: block; width: 100%; height: 100%; } #pgc-card .pgc-card-href:hover { text-decoration: none; } /*pc 樣式*/ .pgc-card { box-sizing: border-box; height: 164px; border: 1px solid #e8e8e8; position: relative; padding: 20px 94px 12px 180px; overflow: hidden; } .pgc-card::after { content: " "; display: block; border-left: 1px solid #e8e8e8; height: 120px; position: absolute; right: 76px; top: 20px; } .pgc-cover { position: absolute; width: 162px; height: 162px; top: 0; left: 0; background-size: cover; } .pgc-content { overflow: hidden; position: relative; top: 50%; -webkit-transform: translateY(-50%); transform: translateY(-50%); } .pgc-content-title { font-size: 18px; color: #222; line-height: 1; font-weight: bold; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } .pgc-content-desc { font-size: 14px; color: #444; overflow: hidden; text-overflow: ellipsis; padding-top: 9px; overflow: hidden; line-height: 1.2em; display: -webkit-inline-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; } .pgc-content-price { font-size: 22px; color: #f85959; padding-top: 18px; line-height: 1em; } .pgc-card-buy { width: 75px; position: absolute; right: 0; top: 50px; color: #406599; font-size: 14px; text-align: center; } .pgc-buy-text { padding-top: 10px; } .pgc-icon-buy { height: 23px; width: 20px; display: inline-block; background: url(https://lf3-cdn-tos.bytescm.com/obj/cdn-static-resource/pgc/v2/pgc_tpl/static/image/commodity_buy_f2b4d1a.png); }

非數值型

對於非數值型數據(例如,字符串或日期數據),describe() 方法返回的統計信息可能不同,通常包括:

  1. count: 非空值的數量。
  2. unique: 唯一值的數量。
  3. top: 出現頻率最高的值。
  4. freq: 出現頻率最高的值出現的次數。
import pandas as pd
# 創建一個包含非數值型數據的 DataFrame
data = {
    'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve'],
    'City': ['New York', 'Los Angeles', 'New York', 'Chicago', 'Los Angeles']
}
df = pd.DataFrame(data)
# 顯示非數值型數據的統計信息
df.describe(include=[object])
         Name      City
count       5         5
unique      5         3
top     Alice  New York
freq        1         2

好啦,以上就是對describe函數的介紹,喜歡的朋友就點個贊吧~