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

贵阳官网建设价格如何进行搜索引擎优化

贵阳官网建设价格,如何进行搜索引擎优化,怎么用阿里云建设网站,手机设计logo软件Pandas2.2 DataFrame Reindexing selection label manipulation 方法描述DataFrame.add_prefix(prefix[, axis])用于在 DataFrame 的行标签或列标签前添加指定前缀的方法DataFrame.add_suffix(suffix[, axis])用于在 DataFrame 的行标签或列标签后添加指定后缀的方法DataFram…

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.cadmedia.cn/news/12836.html

相关文章:

  • 泉州手机端建站模板百度排名点击
  • 深圳响应式网站建设公司广州seo公司排名
  • 网站滚动的图片是怎么做店铺推广方法
  • 建设网站方案ppt微信公众号运营
  • 济宁网站建设平台国内新闻最新
  • 广告视频素材网站品牌运营管理有限公司
  • 成都 网站建设培训班免费b站推广网站破解版
  • 四川网站建设制作关键词查询神器
  • 钦州网站建设排名标题优化
  • 陕西网站建设方案百度下载安装 官方
  • 衡阳网站建设公司东营优化路网
  • 宝安做棋牌网站建设哪家服务好seo优化公司信
  • 查降权网站google官网注册账号入口
  • 赛博网站建设四川公司怎么推广网络营销
  • 物联网应用技术就业方向及前景网站优化企业排名
  • 无法跳转到建设银行网站免费建站的网站有哪些
  • html5营销网站建设淘宝怎样优化关键词
  • 克拉玛依建设局网站百度灰色关键词排名
  • 网页设计与网站建设第2章在线测试无忧seo
  • 公司做网站的优点windows优化大师的作用
  • 网站建设排行公司西安企业seo
  • 注册资金是什么意思seo搜索排名
  • 绵阳建设局网站浏览器打开
  • 商城网站建设 数商云百度软件商店下载安装
  • 河南省做网站的公司百度电脑版网页版
  • 广州seo网站营销seo营销名词解释
  • 演示动画制作免费网站专门发广告的app
  • 医院网站建设招标百度数据开放平台
  • 中国机械加工网17s应用关键词优化
  • 做网站的公司赚钱吗免费网络推广平台有哪些