Reputation: 301
I am running a np.random.choice like the one below.
record = np.random.choice(data, size=6, p=prob)
maxv = max(record)
minv = min(record)
val = record
From this I am finding the min and the max. I want to join this to an pandas dataframe. Below is my desired output:
Min,Max,value
1,5,2
1,5,3
1,5,3
1,5,5
1,5,1
1,5,3
This is an example of the output I would like from one simulation. Keep in mind I am performing this simulation many times so I would like to continuously be able to add onto the dataframe that is created. Each simulation will have its own min and max respectively. I also would like to keep the min and max in the output (why 1 and 5 are in the example output).
Upvotes: 1
Views: 536
Reputation: 393963
I'd create the df with the initial data column 'Val' and then just add the new columns in a one liner:
In [242]:
df = pd.DataFrame({'Val':np.random.randint(1,6,6)})
df['Min'], df['Max'] = df['Val'].min(), df['Val'].max()
df
Out[242]:
Val Min Max
0 4 2 5
1 5 2 5
2 5 2 5
3 4 2 5
4 5 2 5
5 2 2 5
Upvotes: 1
Reputation: 107567
Simply iterate through simulation and append values into dataframe:
# CREATE DATA FRAME STRUCTURE
df = pd.DataFrame(columns=['Min', 'Max', 'val'])
# RUN SIMULATION IN LOOP ITERATION
record = np.random.choice(data, size=6, p=prob)
for i in range(len(record)):
maxv = np.max(record)
minv = np.min(record)
val = record[i]
# APPEND ROW
df.loc[len(df)] = [maxv, minv, val]
Upvotes: 0
Reputation: 50
This is how I solve it:
record = np.random.choice(data, size=6, p=prob)
maxv = [max(record)] * len(record)
minv = [min(record)] * len(record)
new_data = zip(minv, maxv, record)
df = DataFrame(new_data, columns=['Min', 'Max', 'val'])
Upvotes: 0