[英]How to use one dataset to simulate another using Python?
I have the following example df:我有以下示例df:
import pandas as pd
cars = {'Brand': ['Honda Civic','Toyota Corolla','Ford Focus','Audi A4'],
'Price': [22000,25000,27000,35000]
}
df = pd.DataFrame(cars, columns = ['Brand','Price'], index=['Car_1','Car_2','Car_3','Car_4'])
Is there a way to generate a new dataset with parameters from the first one?有没有办法用第一个参数生成一个新的数据集? I want the 'Brand' values to be the same, but 'Price' values to be randomly generated by using the min.,max, and mean of the original values in 'Price'.
我希望“品牌”值相同,但“价格”值是通过使用“价格”中原始值的最小值、最大值和平均值随机生成的。
I can code this manually, but wondering if there is a way to have python detect the parameters of a dataframe and create a new one, with those parameters.我可以手动编码,但想知道是否有办法让 python 检测 dataframe 的参数并使用这些参数创建一个新参数。
Note: the example dataframe I provided is tiny, but these concepts would apply to bigger data.注意:我提供的示例 dataframe 很小,但这些概念适用于更大的数据。
You can use np.random.randint.您可以使用 np.random.randint。 The first 2 parameters set the bounds, and the third gives the number of values.
前两个参数设置界限,第三个参数给出值的数量。
import pandas as pd
import numpy as np
df2 = pd.DataFrame({'Brand': df['Brand'],
'Price': np.random.randint(df['Price'].min(),
df['Price'].max(), df.shape[0])})
print(df2)
Brand Price
Car_1 Honda Civic 29797
Car_2 Toyota Corolla 33306
Car_3 Ford Focus 26237
Car_4 Audi A4 23580
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.