当前位置:网站首页>解决dataframe报错ValueError: Cannot take a larger sample than population when ‘replace=False‘
解决dataframe报错ValueError: Cannot take a larger sample than population when ‘replace=False‘
2022-06-24 19:30:00 【呆萌的代Ma】
完整报错
ValueError: Cannot take a larger sample than population when 'replace=False'
解决方法
在抽样时目标样本数大于已有的样本时就会报错,需要根据业务自行判断,比如下面是一种解决方法
import pandas as pd
import numpy as np
dataframe = pd.DataFrame(np.random.random(size=(10, 2)))
# 开始抽样
sample_df = dataframe.sample(20) if dataframe.shape[0] > 20 else dataframe # 如果采样数量超过有的样本,就全部返回,否则只返回20个
print(sample_df.shape)
sample_df = dataframe.sample(6) if dataframe.shape[0] > 6 else dataframe
print(sample_df.shape)
问题解析
使用如下代码会导致报错:
import pandas as pd
import numpy as np
dataframe = pd.DataFrame(np.random.random(size=(10, 2)))
dataframe.sample(20)
边栏推荐
- leetcode_ one thousand three hundred and sixty-five
- 如何化解35岁危机?华为云数据库首席架构师20年技术经验分享
- [featured] how do you design unified login with multiple accounts?
- 机器学习:线性回归
- How to achieve energy conservation and environmental protection of full-color outdoor LED display
- Reduce the pip to the specified version (upgrade the PIP through CMP and reduce it to the original version)
- Reduce the pip to the specified version (upgrade the PIP through pycharm, and then reduce it to the original version)
- dp问题集
- Kubernetes 集群中流量暴露的几种方案
- Structured interview of state-owned enterprises and central enterprises employment of state-owned enterprises Modou Interactive Employment Service steward
猜你喜欢
随机推荐
是真干不过00后,给我卷的崩溃,想离职了...
想当测试Leader,这6项技能你会吗?
[camera Foundation (I)] working principle and overall structure of camera
Kubernetes 集群中流量暴露的几种方案
The process from troubleshooting to problem solving: the browser suddenly failed to access the web page, error code: 0x80004005, and the final positioning: "when the computer turns on the hotspot, the
Principle and application of queue implementation
[200 opencv routines] 209 Color image segmentation in HSV color space
The leader of ERP software in printing industry
Object. Defineproperty and reflect Fault tolerance of defineproperty
leetcode1863_ 2021-10-14
Machine learning: gradient descent method
The collection of zero code enterprise application cases in various industries was officially released
Excel布局
60 个神级 VS Code 插件!!
“阿里健康”们的逻辑早就变了
面试官:你说你精通Redis,你看过持久化的配置吗?
【无标题】
[notes of Wu Enda] multivariable linear regression
Suspend component and asynchronous component
滤波数据分析








