当前位置:网站首页>Valueerror: cannot take a larger sample than population when 'replace=false‘

Valueerror: cannot take a larger sample than population when 'replace=false‘

2022-06-24 22:17:00 Dai Meng Ma

Complete error report

ValueError: Cannot take a larger sample than population when 'replace=False'

resolvent

An error will be reported when the number of target samples is greater than the existing samples , You need to make your own judgment based on your business , For example, the following is a solution

import pandas as pd
import numpy as np

dataframe = pd.DataFrame(np.random.random(size=(10, 2)))

#  Start sampling 
sample_df = dataframe.sample(20) if dataframe.shape[0] > 20 else dataframe #  If the number of samples exceeds some samples , Just go back , Otherwise, only return to 20 individual 
print(sample_df.shape)

sample_df = dataframe.sample(6) if dataframe.shape[0] > 6 else dataframe
print(sample_df.shape)

Problem analysis

Using the following code will result in an error :

import pandas as pd
import numpy as np

dataframe = pd.DataFrame(np.random.random(size=(10, 2)))
dataframe.sample(20)
原网站

版权声明
本文为[Dai Meng Ma]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241537061090.html