sample

How do I aggregate 5 random samples of a dict?

How do I aggregate 5 random samples of a dict? Question: I’m trying to sample 5 items from a dict then aggregate them by sum. In this case by building a sum of all 5 prices. How do I do that? import random mydict = { 1: {"name":"item1","price": 16}, 2: {"name":"item2","price": 14}, 3: {"name":"item3","price": 16}, …

Total answers: 3

How to get sample of data by having best possible equal number of rows from multiple columns?

How to get sample of data by having best possible equal number of rows from multiple columns? Question: Data: df1 = pd.DataFrame(np.random.randint(0,1000,size=(100, 4)), columns=list(‘ABCD’)) df1["cat1"] = np.random.choice([‘a’, ‘b’], len(df1)) df1["cat2"] = np.random.choice([‘32782’, ‘35871’, ‘35865’], len(df1)) df1["cat3"] = np.random.choice([‘pq’, ‘xy’, ‘ab’, ‘hq’], len(df1)) I want to take sample of this dataset i.e., 200 rows by having …

Total answers: 1

'Oversampling' cartesian data in a dataframe without for loop?

'Oversampling' cartesian data in a dataframe without for loop? Question: I have a 3D data in a pandas dataframe that I would like to ‘oversample’/smooth by replacing the value at each x,y point with the average value of all the points that are within 5 units of that point. I can do it using a …

Total answers: 2

How to randomly sample and keep only n values of repeating IDs?

How to randomly sample and keep only n values of repeating IDs? Question: I have a data frame that looks like this: user_id tweet_id tweet user123 7658j dogs are super user245 66721 yes dogs are super user245 6d343 yes cats are also super <…> <…> <…> user245 541238 well I developed allergy on cates As …

Total answers: 1

Random Sample of a subset of a dataframe in Pandas

Random Sample of a subset of a dataframe in Pandas Question: I have a pandas DataFrame with 100,000 rows and want to split it into 100 sections with 1000 rows in each of them. How do I draw a random sample of certain size (e.g. 50 rows) of just one of the 100 sections? The …

Total answers: 5

How to incrementally sample without replacement?

How to incrementally sample without replacement? Question: Python has my_sample = random.sample(range(100), 10) to randomly sample without replacement from [0, 100). Suppose I have sampled n such numbers and now I want to sample one more without replacement (without including any of the previously sampled n), how to do so super efficiently? update: changed from …

Total answers: 13

How to generate a random list of fixed length of values from given range?

How to generate a random list of fixed length of values from given range? Question: How to generate a random (but unique and sorted) list of a fixed given length out of numbers of a given range in Python? Something like that: >>> list_length = 4 >>> values_range = [1, 30] >>> random_list(list_length, values_range) [1, …

Total answers: 4