当前位置:首页>python>Python --->numpy+pandas+matplotlib

Python --->numpy+pandas+matplotlib

  • 2026-10-11 07:31:03
Python --->numpy+pandas+matplotlib

一、NumPy 数值计算库

导入:import numpy as np

功能
函数 / 写法
示例
创建数组
np.array()arr = np.array([20,40,60,80])
求和
np.sum()np.sum(arr)
平均值
np.mean()np.mean(arr)
最大值 / 最小值
np.max()
 / np.min()
np.max(arr)
标准差
np.std()np.std(arr)
条件筛选
数组布尔索引
arr[arr > 50]
判断空值
np.isnan()np.isnan(arr)

二、Pandas 数据分析库(核心)

导入:import pandas as pd

1. 文件读写

  • 读取 Excel:df = pd.read_excel("文件.xlsx")
  • 读取 CSV:df = pd.read_csv("文件.csv")
  • 导出文件:df.to_excel("结果.xlsx", index=False)

2. 数据查看

  • 查看前 N 行:df.head()
  • 字段类型 / 行数:df.info()
  • 数值统计:df.describe()
  • 查看行列数:df.shape
  • 查看列名:df.columns

3. 数据清洗

  • 去重:df.drop_duplicates()
  • 删除空值:df.dropna()
  • 填充空值:df.fillna(0)

4. 数据筛选

  • 条件过滤:df[df["金额"] > 100]

5. 分组聚合

  • 求和:df.groupby("品类")["金额"].sum()
  • 去重计数:df.groupby("日期")["用户ID"].nunique()

6. 新增字段

  • 衍生指标:df["转化率"] = df["付款数"] / df["浏览数"]

7. 时间处理

  • 转为时间格式:df["时间"] = pd.to_datetime(df["下单时间"])
  • 提取小时:df["小时"] = df["时间"].dt.hour

三、Matplotlib 可视化库

导入:import matplotlib.pyplot as plt

功能
函数 / 写法
示例
折线图 (看趋势)
plt.plot()plt.plot([1,2,3],[10,20,15])
柱状图 (做对比)
plt.bar()plt.bar(["午市","晚市"],[120,90])
设置标题
plt.title()plt.title("每日销量")
设置坐标轴
plt.xlabel()
 / plt.ylabel()
plt.xlabel("日期")
展示图表
plt.show()plt.show()
保存图片
plt.savefig("图表.png")plt.savefig("销量图.png")

最新文章

随机文章