Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

การสรุปข้อมูล DataFrame ด้วย describe() ใน Pandas Python

ฟังก์ชัน describe() ให้สถิติพื้นฐานของ DataFrame ทั้ง Numeric และ Object Columns ได้ในครั้งเดียว ช่วย EDA (Exploratory Data Analysis) ให้รวดเร็ว

ตัวอย่างโค้ด


import pandas as pd

my_data = {
    'Name': pd.Series(['Tom', 'Jane', 'Vin', 'Eve', 'Will']),
    'Age': pd.Series([45, 67, 89, 12, 23]),
    'value': pd.Series([8.79, 23.24, 31.98, 78.56, 90.20])
}

my_df = pd.DataFrame(my_data)
print("The dataframe is:")
print(my_df)

print("\nThe description of data is:")
print(my_df.describe())

ผลลัพธ์


The dataframe is:
   Name  Age  value
0  Tom   45   8.79
1  Jane  67  23.24
2  Vin   89  31.98
3  Eve   12  78.56
4  Will  23  90.20

The description of data is:
              Age      value
count   5.000000   5.000000
mean   47.200000  46.554000
std    31.499206  35.747102
min    12.000000   8.790000
25%    23.000000  23.240000
50%    45.000000  31.980000
75%    67.000000  78.560000
max    89.000000  90.200000

คำอธิบายสถิติแต่ละตัว

ตัวเลือก describe()


# รวม Object/String Columns
df.describe(include='all')
df.describe(include=[np.number, 'object'])

# เปลี่ยน Percentiles
df.describe(percentiles=[0.1, 0.5, 0.9])

# เฉพาะ Column บางตัว
df[['Age', 'value']].describe()

describe() สำหรับ String/Object Columns


print(my_df['Name'].describe())
# count     5
# unique    5
# top       Eve
# freq      1

สถิติเพิ่มเติมสำหรับ Object: unique (ค่าที่ไม่ซ้ำ), top (ค่าที่พบมากที่สุด), freq (ความถี่ของ top)

การใช้งานจริง


# Quick EDA
print(df.describe(include='all').T)  # Transpose เพื่ออ่านง่าย

# ตรวจสอบ Missing Values
print(df.isnull().sum())

# ตรวจสอบ Data Types
print(df.dtypes)
สถิติคำอธิบาย
countจำนวนค่า Non-null
meanค่าเฉลี่ยเลขคณิต
stdส่วนเบี่ยงเบนมาตรฐาน (Sample Std, ddof=1)
minค่าต่ำสุด
25%Percentile 25 (Q1)
50%Median (Q2)
75%Percentile 75 (Q3)
maxค่าสูงสุด