跳到主要内容

数据分析入门项目

用Python进行数据处理和可视化

项目概述

使用Python进行基础数据分析,学习Pandas和Matplotlib。

环境准备

pip install pandas matplotlib

基础数据分析

import pandas as pd
import matplotlib.pyplot as plt

# 读取CSV数据
df = pd.read_csv("sales.csv")

# 基本信息
print(df.head()) # 前5行
print(df.info()) # 数据类型
print(df.describe()) # 统计摘要

# 数据筛选
high_sales = df[df["销售额"] > 1000]

# 分组统计
by_category = df.groupby("类别")["销售额"].sum()

# 可视化
plt.figure(figsize=(10, 6))
by_category.plot(kind="bar")
plt.title("各类别销售额")
plt.savefig("chart.png")
plt.show()

实战:分析销售数据

# 创建示例数据
data = {
"日期": pd.date_range("2024-01-01", periods=30),
"销售额": [100, 150, 120, 180, 200, 160, 140, 190, 210, 170,
155, 165, 185, 195, 175, 145, 135, 125, 115, 205,
215, 225, 235, 245, 220, 230, 240, 250, 260, 270]
}
df = pd.DataFrame(data)

# 分析
print(f"总销售额: {df['销售额'].sum()}")
print(f"平均日销售: {df['销售额'].mean():.2f}")
print(f"最高单日: {df['销售额'].max()}")

# 绘制趋势图
plt.plot(df["日期"], df["销售额"])
plt.title("销售趋势")
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()

本章小结

  1. Pandas:数据读取、清洗、分析
  2. Matplotlib:数据可视化
  3. 数据分析流程:读取→清洗→分析→可视化

→ 继续阅读:30-网页爬虫实战