数据分析是 Python 应用领域的一个重要分支,Python 提供了丰富的库来帮助开发者进行数据分析和处理。以下是一些常用的 Python 数据分析库和它们的基本使用方法。

常用库

  • Pandas: 用于数据清洗、转换和分析。
  • NumPy: 用于数值计算。
  • Matplotlib: 用于数据可视化。
  • Scikit-learn: 用于机器学习。

Pandas

Pandas 是 Python 中最常用的数据分析库之一,它提供了强大的数据处理功能。

安装

pip install pandas

使用示例

import pandas as pd

# 创建一个 DataFrame
data = {
    'Name': ['Alice', 'Bob', 'Charlie'],
    'Age': [25, 30, 35],
    'City': ['New York', 'Los Angeles', 'Chicago']
}
df = pd.DataFrame(data)

# 显示 DataFrame
print(df)

NumPy

NumPy 是 Python 中用于数值计算的库。

安装

pip install numpy

使用示例

import numpy as np

# 创建一个数组
array = np.array([1, 2, 3, 4, 5])

# 显示数组
print(array)

Matplotlib

Matplotlib 是 Python 中用于数据可视化的库。

安装

pip install matplotlib

使用示例

import matplotlib.pyplot as plt

# 创建一个简单的折线图
plt.plot([1, 2, 3, 4, 5], [1, 4, 9, 16, 25])
plt.show()

Scikit-learn

Scikit-learn 是 Python 中用于机器学习的库。

安装

pip install scikit-learn

使用示例

from sklearn.linear_model import LinearRegression

# 创建一个线性回归模型
model = LinearRegression()

# 训练模型
model.fit([[1, 2], [2, 3], [3, 4]], [1, 4, 9])

# 预测
print(model.predict([[4, 5]]))

更多关于 Python 数据分析的内容,您可以访问我们的Python 数据分析教程页面。

图片展示

  • Pandas
  • NumPy
  • Matplotlib
  • Scikit-learn