python:chapter38

第三十八章:数据分析项目

完成本章学习后,你将能够:

  • 使用NumPy处理数值数据
  • 使用Pandas处理表格数据
  • 使用Matplotlib可视化
  • 完成完整数据分析流程
import numpy as np
 
# 创建数组
arr = np.array([1, 2, 3, 4, 5])
matrix = np.array([[1, 2], [3, 4], [5, 6]])
 
# 特殊数组
zeros = np.zeros((3, 4))
ones = np.ones((2, 3))
identity = np.eye(3)
 
# 数组运算
arr + 10
arr * 2
np.sum(arr)
np.mean(arr)
np.std(arr)
 
# 索引和切片
arr[0]
matrix[0, 1]
matrix[:, 0]  # 第一列
import pandas as pd
 
# Series
s = pd.Series([1, 3, 5, np.nan, 6])
 
# DataFrame
df = pd.DataFrame({
    'A': [1, 2, 3, 4],
    'B': ['a', 'b', 'c', 'd'],
    'C': [1.0, 2.0, 3.0, 4.0]
})
 
# 读取数据
df = pd.read_csv('data.csv')
df = pd.read_excel('data.xlsx')
 
# 数据查看
print(df.head())
print(df.info())
print(df.describe())
 
# 筛选
df[df['A'] > 2]
df.query('A > 2')
# 处理缺失值
df.dropna()  # 删除
df.fillna(0)  # 填充
 
# 去重
df.drop_duplicates()
 
# 类型转换
df['A'] = df['A'].astype(int)
 
# 新增列
df['D'] = df['A'] + df['C']
 
# 分组统计
df.groupby('B')['A'].mean()
import matplotlib.pyplot as plt
 
# 折线图
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Plot')
plt.show()
 
# 散点图
plt.scatter(df['A'], df['C'])
 
# 柱状图
plt.bar(df['B'], df['A'])
 
# DataFrame直接绘图
df.plot(kind='bar')

1. 销售数据分析 2. 股票数据分析 3. 用户行为分析

下一章:第三十九章:Web应用项目

该主题尚不存在

您访问的页面并不存在。如果允许,您可以使用创建该页面按钮来创建它。

  • python/chapter38.txt
  • 最后更改: 2026/04/09 14:42
  • 张叶安