Python数据分析基础:使用NumPy和Pandas进行数据处理
数据分析是从数据中提取有价值信息的过程,Python是数据分析的常用工具,因为它有强大的数据分析库支持。

Python数据分析
1. 环境准备
安装必要的库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| # 安装NumPy,用于数值计算
pip install numpy
# 安装Pandas,用于数据处理和分析
pip install pandas
# 安装Matplotlib,用于数据可视化
pip install matplotlib
# 安装Seaborn,用于高级数据可视化
pip install seaborn
# 安装Jupyter Notebook,用于交互式数据分析
pip install jupyter
|
2. NumPy基础
NumPy(Numerical Python)是Python的一个数值计算库,它提供了高性能的多维数组对象和用于处理这些数组的工具。
创建NumPy数组
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
| import numpy as np
# 创建一维数组
arr1 = np.array([1, 2, 3, 4, 5])
print(f'一维数组: {arr1}')
print(f'数组形状: {arr1.shape}')
# 创建二维数组
arr2 = np.array([[1, 2, 3], [4, 5, 6]])
print(f'\n二维数组:\n{arr2}')
print(f'数组形状: {arr2.shape}')
# 创建全零数组
zeros = np.zeros((2, 3))
print(f'\n全零数组:\n{zeros}')
# 创建全一数组
ones = np.ones((2, 3))
print(f'\n全一数组:\n{ones}')
# 创建单位矩阵
identity = np.eye(3)
print(f'\n单位矩阵:\n{identity}')
# 创建等差数列
arange = np.arange(0, 10, 2)
print(f'\n等差数列: {arange}')
# 创建等间隔数组
linspace = np.linspace(0, 1, 5)
print(f'\n等间隔数组: {linspace}')
|
NumPy数组运算
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| # 数组运算
arr = np.array([1, 2, 3, 4, 5])
print(f'原数组: {arr}')
print(f'数组+1: {arr + 1}')
print(f'数组*2: {arr * 2}')
print(f'数组平方: {arr ** 2}')
print(f'数组开方: {np.sqrt(arr)}')
print(f'数组正弦值: {np.sin(arr)}')
# 数组间运算
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
print(f'\n数组1: {arr1}')
print(f'数组2: {arr2}')
print(f'数组1+数组2: {arr1 + arr2}')
print(f'数组1*数组2: {arr1 * arr2}')
print(f'数组点积: {np.dot(arr1, arr2)}')
|
数组索引和切片
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| # 一维数组索引和切片
arr = np.array([0, 1, 2, 3, 4, 5])
print(f'原数组: {arr}')
print(f'索引2: {arr[2]}')
print(f'切片1-4: {arr[1:4]}')
print(f'每隔一个元素: {arr[::2]}')
# 二维数组索引和切片
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(f'\n原数组:\n{arr}')
print(f'索引[1, 2]: {arr[1, 2]}')
print(f'切片行1: {arr[1, :]}')
print(f'切片列1: {arr[:, 1]}')
print(f'切片子数组:\n{arr[0:2, 0:2]}')
|
3. Pandas基础
Pandas是Python的一个数据分析库,它提供了高性能、易用的数据结构和数据分析工具。
Series对象
Series是一种一维标记数组,类似于带有索引的列表。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| import pandas as pd
# 创建Series
# 从列表创建
s = pd.Series([1, 3, 5, np.nan, 6, 8])
print(f'Series:\n{s}')
# 从字典创建
data = {'a': 0., 'b': 1., 'c': 2.}
s = pd.Series(data)
print(f'\n从字典创建的Series:\n{s}')
# 指定索引
s = pd.Series([1, 3, 5], index=['x', 'y', 'z'])
print(f'\n指定索引的Series:\n{s}')
# Series操作
print(f'\nSeries值: {s.values}')
print(f'Series索引: {s.index}')
print(f'Series前2个元素:\n{s.head(2)}')
print(f'Series后2个元素:\n{s.tail(2)}')
print(f'Series描述统计:\n{s.describe()}')
|
DataFrame对象
DataFrame是一种二维标记数据结构,类似于表格或Excel工作表。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| # 创建DataFrame
# 从字典创建
data = {
'name': ['Alice', 'Bob', 'Charlie', 'David'],
'age': [25, 30, 35, 40],
'city': ['New York', 'London', 'Paris', 'Tokyo'],
'salary': [50000, 60000, 70000, 80000]
}
df = pd.DataFrame(data)
print(f'DataFrame:\n{df}')
# 从CSV文件读取
# df = pd.read_csv('data.csv')
# 从Excel文件读取
# df = pd.read_excel('data.xlsx')
# DataFrame操作
print(f'\nDataFrame形状: {df.shape}')
print(f'DataFrame列名: {df.columns}')
print(f'DataFrame索引: {df.index}')
print(f'\nDataFrame前2行:\n{df.head(2)}')
print(f'\nDataFrame后2行:\n{df.tail(2)}')
print(f'\nDataFrame描述统计:\n{df.describe()}')
print(f'\nDataFrame信息:\n')
df.info()
|
DataFrame索引和选择
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| # 选择列
print(f'\n选择name列:\n{df["name"]}')
print(f'\n选择name和age列:\n{df[["name", "age"]]}')
# 选择行
print(f'\n使用loc选择行:\n{df.loc[0]}')
print(f'\n使用loc选择多行:\n{df.loc[[0, 2]]}')
print(f'\n使用loc选择行和列:\n{df.loc[0:2, ["name", "age"]]}')
# 使用iloc选择
print(f'\n使用iloc选择行:\n{df.iloc[0]}')
print(f'\n使用iloc选择多行:\n{df.iloc[0:2]}')
print(f'\n使用iloc选择行和列:\n{df.iloc[0:2, 0:2]}')
# 条件选择
print(f'\n年龄大于30的行:\n{df[df["age"] > 30]}')
print(f'\n年龄大于30且在纽约的行:\n{df[(df["age"] > 30) & (df["city"] == "New York")]}')
|
数据清洗
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
| # 处理缺失值
# 创建包含缺失值的DataFrame
data = {
'name': ['Alice', 'Bob', None, 'David'],
'age': [25, None, 35, 40],
'city': ['New York', 'London', 'Paris', None],
'salary': [50000, 60000, None, 80000]
}
df = pd.DataFrame(data)
print(f'包含缺失值的DataFrame:\n{df}')
# 检测缺失值
print(f'\n缺失值检测:\n{df.isnull()}')
print(f'\n缺失值数量:\n{df.isnull().sum()}')
# 删除缺失值
print(f'\n删除缺失值:\n{df.dropna()}')
# 填充缺失值
print(f'\n填充缺失值:\n{df.fillna(0)}')
print(f'\n使用均值填充数值列:\n{df.fillna(df.mean(numeric_only=True))}')
# 数据去重
data = {
'name': ['Alice', 'Bob', 'Alice', 'David'],
'age': [25, 30, 25, 40],
'city': ['New York', 'London', 'New York', 'Tokyo'],
'salary': [50000, 60000, 50000, 80000]
}
df = pd.DataFrame(data)
print(f'包含重复值的DataFrame:\n{df}')
print(f'\n重复值检测:\n{df.duplicated()}')
print(f'\n删除重复值:\n{df.drop_duplicates()}')
|
数据转换
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
| # 添加新列
df['bonus'] = df['salary'] * 0.1
print(f'添加bonus列:\n{df}')
# 应用函数
df['salary_level'] = df['salary'].apply(lambda x: 'high' if x > 60000 else 'medium' if x > 50000 else 'low')
print(f'\n添加salary_level列:\n{df}')
# 分组聚合
grouped = df.groupby('salary_level')
print(f'\n按salary_level分组的平均工资:\n{grouped['salary'].mean()}')
print(f'\n按salary_level分组的工资总和:\n{grouped['salary'].sum()}')
print(f'\n按salary_level分组的统计信息:\n{grouped.describe()}')
# 数据排序
print(f'\n按salary降序排序:\n{df.sort_values(by='salary', ascending=False)}')
# 数据合并
# 创建两个DataFrame
df1 = pd.DataFrame({
'name': ['Alice', 'Bob', 'Charlie'],
'age': [25, 30, 35]
})
df2 = pd.DataFrame({
'name': ['Alice', 'Bob', 'David'],
'salary': [50000, 60000, 70000]
})
# 内连接
print(f'\n内连接:\n{pd.merge(df1, df2, on='name', how='inner')}')
# 左连接
print(f'\n左连接:\n{pd.merge(df1, df2, on='name', how='left')}')
# 右连接
print(f'\n右连接:\n{pd.merge(df1, df2, on='name', how='right')}')
# 外连接
print(f'\n外连接:\n{pd.merge(df1, df2, on='name', how='outer')}')
|
4. 数据可视化
使用Matplotlib
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
| import matplotlib.pyplot as plt
# 折线图
x = np.arange(0, 10, 0.1)
y = np.sin(x)
plt.plot(x, y)
plt.title('Sin Wave')
plt.xlabel('x')
plt.ylabel('sin(x)')
plt.grid(True)
plt.show()
# 散点图
df = pd.DataFrame({
'x': np.random.randn(100),
'y': np.random.randn(100)
})
plt.scatter(df['x'], df['y'])
plt.title('Scatter Plot')
plt.xlabel('x')
plt.ylabel('y')
plt.grid(True)
plt.show()
# 直方图
plt.hist(df['x'], bins=20)
plt.title('Histogram')
plt.xlabel('x')
plt.ylabel('Frequency')
plt.grid(True)
plt.show()
# 条形图
categories = ['A', 'B', 'C', 'D', 'E']
values = [10, 20, 15, 25, 30]
plt.bar(categories, values)
plt.title('Bar Chart')
plt.xlabel('Category')
plt.ylabel('Value')
plt.grid(True, axis='y')
plt.show()
|
使用Seaborn
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
| import seaborn as sns
# 设置样式
sns.set_style('whitegrid')
# 散点图
sns.scatterplot(x='x', y='y', data=df)
plt.title('Scatter Plot with Seaborn')
plt.show()
# 直方图
sns.histplot(df['x'], bins=20, kde=True)
plt.title('Histogram with Seaborn')
plt.show()
# 箱线图
sns.boxplot(x='salary_level', y='salary', data=df)
plt.title('Box Plot with Seaborn')
plt.show()
# 热力图
corr = df.corr()
sns.heatmap(corr, annot=True, cmap='coolwarm')
plt.title('Correlation Heatmap')
plt.show()
# 成对关系图
sns.pairplot(df)
plt.show()
|
5. 实战:分析iris数据集
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
| # 加载iris数据集
from sklearn.datasets import load_iris
iris = load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)
df['species'] = iris.target_names[iris.target]
# 数据探索
print(f'iris数据集形状: {df.shape}')
print(f'\niris数据集前5行:\n{df.head()}')
print(f'\niris数据集描述统计:\n{df.describe()}')
print(f'\niris数据集信息:\n')
df.info()
print(f'\niris数据集物种分布:\n{df['species'].value_counts()}')
# 数据可视化
# 成对关系图
sns.pairplot(df, hue='species')
plt.title('Iris Pairplot')
plt.show()
# 箱线图
plt.figure(figsize=(10, 6))
for i, feature in enumerate(iris.feature_names, 1):
plt.subplot(2, 2, i)
sns.boxplot(x='species', y=feature, data=df)
plt.title(f'{feature} by Species')
plt.tight_layout()
plt.show()
# 热力图
corr = df.drop('species', axis=1).corr()
sns.heatmap(corr, annot=True, cmap='coolwarm')
plt.title('Iris Correlation Heatmap')
plt.show()
|
6. 推荐学习资源
Details
| 库名 | 用途 |
|---|
| NumPy | 数值计算、数组操作 |
| Pandas | 数据处理、数据分析 |
| Matplotlib | 数据可视化 |
| Seaborn | 高级数据可视化 |
| Scikit-learn | 机器学习 |
| TensorFlow | 深度学习 |
| PyTorch | 深度学习 |
| XGBoost | 梯度提升算法 |
| LightGBM | 梯度提升算法 |
| Statsmodels | 统计建模 |
7. 总结
通过本教程,你已经学会了使用Python进行数据分析的基础知识,包括:
- NumPy基础(数组创建、运算、索引和切片)
- Pandas基础(Series和DataFrame对象)
- 数据清洗(处理缺失值、重复值)
- 数据转换(添加列、应用函数、分组聚合)
- 数据可视化(Matplotlib和Seaborn)
- 实战案例(iris数据集分析)
Python数据分析是一个广泛的领域,掌握这些基础知识将为你进一步学习数据分析和机器学习打下坚实的基础。