Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Phân tích và Trực quan hóa dữ liệu với Python (Pandas & Matplotlib)

Python là ngôn ngữ hàng đầu cho Data Analysis nhờ hệ sinh thái thư viện mạnh: Pandas (xử lý dữ liệu), Matplotlib/Seaborn (trực quan hóa).

Cài đặt

pip install pandas matplotlib

1. Tạo DataFrame từ dữ liệu Python

import pandas as pd

hafeez = ['Hafeez', 19]
aslan = ['Aslan', 21]
kareem = ['Kareem', 18]

df = pd.DataFrame([hafeez, aslan, kareem], columns=['Name', 'Age'])
print(df)

Kết quả

    Name  Age
0  Hafeez   19
1   Aslan   21
2  Kareem   18

2. Đọc dữ liệu từ file CSV

data = pd.read_csv('CountryData.IND.csv')
print(data.head())        # 5 dòng đầu
print(data.shape)         # (rows, cols)
print(data.describe())    # thống kê mô tả

Kết quả shape

(29, 16)

describe() hiển thị count, mean, std, min, quartiles, max cho các cột số.

3. Trực quan hóa với Matplotlib

import matplotlib.pyplot as plt

# Histogram cột 'Time period'
data['Time period'].hist(bins=10)
plt.title('Phân bố Time Period')
plt.xlabel('Time Period')
plt.ylabel('Frequency')
plt.show()

Phân tích và Trực quan hóa dữ liệu với Python (Pandas & Matplotlib)

Các loại biểu đồ phổ biến

LoạiCodeDùng cho
Line plotplt.plot(x, y)Xu hướng theo thời gian
Bar chartplt.bar(categories, values)So sánh nhóm
Scatterplt.scatter(x, y)Mối quan hệ 2 biến
Histogramdf['col'].hist()Phân bố 1 biến
Box plotdf.boxplot(column='col', by='group')Phân bố, outlier theo nhóm
Heatmapsns.heatmap(df.corr())Ma trận tương quan

Workflow phân tích dữ liệu tiêu chuẩn

  1. Load: pd.read_csv/read_excel/read_sql
  2. Inspect: head(), info(), describe(), shape, isnull().sum()
  3. Clean: xử lý missing, duplicate, outlier, type conversion.
  4. Explore (EDA): groupby, pivot_table, correlation, visualization.
  5. Model/Insight: thống kê, ML, báo cáo.
  6. Export: to_csv, to_excel, to_json.

Kết luận

Pandas + Matplotlib là cặp đôi vàng cho Data Analysis trong Python. Hãy thực hành với dataset thực tế (Kaggle, UCI, data.gov) để thành thạo.