[英]Create Panda Df from numpy array
I am running a np.random.choice like the one below. 我正在运行下面的np.random.choice。
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).
我还想在输出中保留最小值和最大值(为什么示例输出中为1和5)。
I'd create the df with the initial data column 'Val' and then just add the new columns in a one liner: 我将使用初始数据列“ Val”创建df,然后将新列添加到一个衬里中:
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
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'])
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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.