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()
Các loại biểu đồ phổ biến
| Loại | Code | Dùng cho |
|---|---|---|
| Line plot | plt.plot(x, y) | Xu hướng theo thời gian |
| Bar chart | plt.bar(categories, values) | So sánh nhóm |
| Scatter | plt.scatter(x, y) | Mối quan hệ 2 biến |
| Histogram | df['col'].hist() | Phân bố 1 biến |
| Box plot | df.boxplot(column='col', by='group') | Phân bố, outlier theo nhóm |
| Heatmap | sns.heatmap(df.corr()) | Ma trận tương quan |
Workflow phân tích dữ liệu tiêu chuẩn
- Load:
pd.read_csv/read_excel/read_sql - Inspect:
head(), info(), describe(), shape, isnull().sum() - Clean: xử lý missing, duplicate, outlier, type conversion.
- Explore (EDA): groupby, pivot_table, correlation, visualization.
- Model/Insight: thống kê, ML, báo cáo.
- 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.