Home > Software engineering >  Generating defined amount of the rows based on max/min of other Dataframe in pandas
Generating defined amount of the rows based on max/min of other Dataframe in pandas

Time:01-23

I have a Dataframe where I have calculated metrics below:

Metrics

I need to generate fixed amount of the rows for new data frame (for example 1000 or 2500), where each row will have a random number no less than minimum and no more than maximum, ideally change for /- 1%.

I was trying solution as below, but without success so far:

Intervals = pd.DataFrame(np.array([[df['Close'].min(), df['Close'].max()],[0.4, 0.6],[0.4, 0.6],[0.20, 1.], [0.3, 0.4], [0.2, 0.3]]))

df = pd.DataFrame(list(Intervals.apply(lambda x: np.random.uniform(low=x[0],high=x[1], size = 2500).T, axis=1)))

print(df.T)

Any ideas how it can be approached?

CodePudding user response:

You can loop over the columns in your metrics dataframe and create an array of random numbers using numpy.random:

pd.DataFrame({
    column: np.random.uniform(
        low=metrics[column].min(), high=metrics[column].max(), size=1000
    )
    for column in metrics.columns
})
  •  Tags:  
  • Related