I appreciate that this type of question has been asked before, but I haven't found a solution to making a random number generator where min and max values are created for each column variable per row.
I am trying to create a dataframe of randomly generated numbers which have ranges that are defined by min and max ranges from values in another dataframe. Each min and max will be different according to the different columns and values per row. I'm also trying to specify how many times to generate random numbers, as well as how many times to multiply the row that the random numbers create. I've tried the following but I'm getting slightly lost and unsure what I need to do next:
import pandas as pd
import numpy as np
import random
multiplier = 2
repeat = 2
df = pd.DataFrame(np.array([[1, 2], [4, 5]]))
headers = list(df)
row_count = len(df.columns[:])
default_values = list(df.iloc[0])
ranges = []
for i in range(row_count):
max_val = df.iloc[:,i] * 1.2
min_val = df.iloc[:,i] * 0.8
ranges.append((min_val, max_val))
values = []
for i in range(repeat):
current_row = []
for j in range(len(ranges)):
min_val, max_val = ranges[j]
current_row.append(round(random.uniform(min_val, max_val),1))
values += [current_row] * multiplier
df2 = pd.DataFrame( values, columns = headers )
Any help would be appreciated!
Aucun commentaire:
Enregistrer un commentaire