简体   繁体   English

如何使用 Python 使用一个数据集来模拟另一个数据集?

[英]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.

相关问题 使用 Python 或 pandas 使用数据集替换另一个数据集中的值 - Use a dataset to replace value in another dataset using Python or pandas 如何使用一个数据集的值作为另一个数据集的过滤器? - How to use one dataset's values as another dataset's filter? 如何提取 PCA 在第一个数据集中使用的参数并将它们应用到另一个使用 Python - how to extract parametres used by PCA in a first dataset and apply them to another one using Python 如何在python中模拟传递函数的一步 - How to simulate one step to a transfer function in python 如何使用 Python 将多个值从一个数据集映射到另一个数据集? - How to map multiple values from 1 dataset to another using Python? 在python中:如何使用描述符来模拟属性 - In python: how to use descriptor to simulate the property 如何使用python根据一列将整个数据集分为4个范围 - How to split the whole dataset into 4 range based on one column using python 如何匹配两个 API 以使用 Python 更新一个 API 数据集 - How to Match two APIs to update one API dataset using Python 如何使用 python 将多个 excel 文档上传到一个数据集中? - How to upload multiple excel documents into one dataset using python? 如何在Python中的类中使用另一个函数 - How to use one function in another with classes in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM