当前位置: 首页 > ai >正文

【Pandas】pandas DataFrame sample

Pandas2.2 DataFrame

Reindexing selection label manipulation

方法描述
DataFrame.add_prefix(prefix[, axis])用于在 DataFrame 的行标签或列标签前添加指定前缀的方法
DataFrame.add_suffix(suffix[, axis])用于在 DataFrame 的行标签或列标签后添加指定后缀的方法
DataFrame.align(other[, join, axis, level, …])用于对齐两个 DataFrameSeries 的方法
DataFrame.at_time(time[, asof, axis])用于筛选 特定时间点 的行的方法
DataFrame.between_time(start_time, end_time)用于筛选 指定时间范围内的数据行 的方法
DataFrame.drop([labels, axis, index, …])用于从 DataFrame 中删除指定行或列的方法
DataFrame.drop_duplicates([subset, keep, …])用于删除重复行的方法
DataFrame.duplicated([subset, keep])用于检测 重复行 的方法
DataFrame.equals(other)用于比较两个 DataFrame 是否完全相等的方法
DataFrame.filter([items, like, regex, axis])用于筛选列或行标签的方法
DataFrame.first(offset)用于选取 时间序列型 DataFrame 中从起始时间开始的一段连续时间窗口 的方法
DataFrame.head([n])用于快速查看 DataFrame 前几行数据 的方法
DataFrame.idxmax([axis, skipna, numeric_only])用于查找 每列或每行中最大值的索引标签 的方法
DataFrame.idxmin([axis, skipna, numeric_only])用于查找 每列或每行中最小值的索引标签 的方法
DataFrame.last(offset)用于选取 时间序列型 DataFrame 中从最后时间点开始向前截取一段指定长度的时间窗口 的方法
DataFrame.reindex([labels, index, columns, …])用于重新索引 DataFrame 的核心方法
DataFrame.reindex_like(other[, method, …])用于将当前 DataFrame 的索引和列重新设置为与另一个对象(如另一个 DataFrame 或 Series)相同
DataFrame.rename([mapper, index, columns, …])用于重命名 DataFrame 的行索引标签或列名的方法
DataFrame.rename_axis([mapper, index, …])用于**重命名 DataFrame 的索引轴名称(index axis name)或列轴名称(column axis name)**的方法
DataFrame.reset_index([level, drop, …])用于将 DataFrame 的索引(index)重置为默认整数索引,并将原索引作为列添加回 DataFrame 中的方法
DataFrame.sample([n, frac, replace, …])用于从 DataFrame 中随机抽取样本行或列的方法

pandas.DataFrame.sample()

pandas.DataFrame.sample() 是一个用于从 DataFrame 中随机抽取样本行或列的方法。它支持按指定数量(n)或比例(frac)抽样,支持有放回或无放回抽样,并可用于数据分析、数据清洗、模型训练前的数据划分等场景。


📌 方法签名
DataFrame.sample(n=None, frac=None, replace=False, weights=None, random_state=None, axis=None, ignore_index=False)

🔧 参数说明
参数类型说明
n整数要抽取的样本数量(不能与 frac 同时使用)
frac浮点数抽取样本占总体的比例(如 0.5 表示抽取 50% 的数据)
replacebool,默认 False是否有放回抽样(True 表示允许重复抽取)
weightsstr 或 array-like权重数组或列名,表示每行/列被抽取的概率权重
random_stateint 或 numpy.random.RandomState 实例控制随机性,确保结果可复现
axis{0/'index', 1/'columns'},默认 0指定是按行抽样还是按列抽样
ignore_indexbool,默认 False是否重置索引(抽样后的 DataFrame 使用从 0 开始的新索引)

⚠️ nfrac 不能同时使用。


✅ 返回值
  • 返回一个新的 DataFrame,包含随机抽取的样本;
  • inplace=True 不可用,必须赋值给新变量;
  • 默认保留原始索引,除非设置 ignore_index=True

🧪 示例代码及结果
示例 1:基本用法 - 随机抽取 2 行
import pandas as pddf = pd.DataFrame({'A': [1, 2, 3, 4],'B': [10, 20, 30, 40]
}, index=['x', 'y', 'z', 'w'])print("Original DataFrame:")
print(df)# 随机抽取 2 行
sampled = df.sample(n=2, random_state=42)
print("\nRandomly sampled 2 rows:")
print(sampled)
输出结果:
Original DataFrame:A   B
x  1  10
y  2  20
z  3  30
w  4  40Randomly sampled 2 rows:A   B
z  3  30
x  1  10

设置 random_state=42 可保证每次运行结果一致。


示例 2:按比例抽样(frac=0.5)
# 抽取 50% 的行
sampled_frac = df.sample(frac=0.5, random_state=42)
print("\nSampled 50% of the rows:")
print(sampled_frac)
输出结果:
Sampled 50% of the rows:A   B
z  3  30
x  1  10

示例 3:有放回抽样(replace=True)
# 从 4 行中抽取 5 行(必须允许重复)
sampled_replace = df.sample(n=5, replace=True, random_state=42)
print("\nSampled with replacement (n=5):")
print(sampled_replace)
输出结果:
Sampled with replacement (n=5):A   B
z  3  30
x  1  10
z  3  30
x  1  10
y  2  20

注意某些行出现多次。


示例 4:加权抽样(weights 参数)
# 给每一行指定不同的权重
sampled_weighted = df.sample(n=2, weights=[1, 1, 1, 10], random_state=42)
print("\nWeighted sampling (last row has higher weight):")
print(sampled_weighted)
输出结果:
Weighted sampling (last row has higher weight):A   B
w  4  40
w  4  40

因为最后一行权重最高,所以更容易被选中。


示例 5:按列抽样(axis=1)
# 随机抽取 1 列
sampled_col = df.sample(n=1, axis=1, random_state=42)
print("\nRandomly sampled 1 column:")
print(sampled_col)
输出结果:
Randomly sampled 1 column:B
x  10
y  20
z  30
w  40

示例 6:忽略原索引(ignore_index=True)
# 抽样并重置索引
sampled_ignore = df.sample(n=2, ignore_index=True, random_state=42)
print("\nSampled and reset index:")
print(sampled_ignore)
输出结果:
Sampled and reset index:A   B
0  3  30
1  1  10

🧠 应用场景
  • 数据探索:快速查看部分数据;
  • 模型训练前的数据划分:随机选取训练集/验证集;
  • 数据增强:通过有放回抽样增加样本量;
  • 测试脚本:模拟小规模数据进行调试;
  • 统计分析:进行抽样调查或蒙特卡洛模拟。

⚠️ 注意事项
  • nfrac 不能同时使用;
  • 若需要重复抽样,需设置 replace=True
  • 使用 random_state 确保结果可复现;
  • 支持按行或列抽样(通过 axis);
  • 默认保留原始索引,可通过 ignore_index=True 重置;
  • 加权抽样时注意权重和应大于 0,否则会报错。
http://www.xdnf.cn/news/11725.html

相关文章:

  • sql server如何创建表导入excel的数据
  • 虚拟斯德哥尔摩症候群:用户为何为缺陷AI辩护?
  • 搭建强化推荐的决策服务架构
  • 【深度学习优化算法】02:凸性
  • 深度探索:如何用DeepSeek重构你的工作流
  • HTML5 网页设计 基础
  • 网页加密/解密( ecode方式)
  • UVM糖果爱好者教程 - 27.打印消息冗余度
  • 使用ExtendSim进行水管理、可持续性和环境仿真建模
  • 推荐5个免费的字体转换网站工具
  • iOS开发_常用的正则表达式
  • MATCH_PARENT与FILL_PARENT:不要做什么和怎么做
  • Android 系统升级流程分析
  • 0-1背包问题(回溯法c++详解)
  • STM32外部挂载SDcard+移植Fatfs文件系统
  • tl r402路由器设置_tplogin.cn登录路由器怎么设置 tplogin.cn登录路由器设置步骤【详解】...
  • Android光线传感器获取光线强弱。LightSensorManager封装类
  • position的relative与absolute的区别
  • 非线性丙类功率放大器实验_倾斜光纤Bragg光栅:抑制高功率光纤激光系统SRS和SBS的理想选择...
  • 网安学习日志01:用kali复现ms17-010漏洞
  • 传奇私服游戏支付接口申请(已解决)
  • iTunes 9.0.3 更新
  • Linux系统服务之inetd
  • 445端口入侵详解
  • 用 Java 实现“人像动漫化”特效
  • Windows 安全基础——NetBIOS篇
  • 硬盘模式JBOD
  • C++实现银行家算法
  • 应用程序发生异常--未知的软件异常怎么办?
  • 【Android动画入门篇】