【问题标题】:How to create a dataframe with simulated data in python如何在 python 中使用模拟数据创建数据框
【发布时间】:2020-07-28 12:11:25
【问题描述】:

我有示例架构,它包含 12 列,每列都有特定的类别。现在我需要将这些数据模拟成一个大约 1000 行的数据框。我该怎么办?

我使用下面的代码为每一列生成数据

      Location = ['USA','India','Prague','Berlin','Dubai','Indonesia','Vienna']
      Location = random.choice(Location)

      Age = ['Under 18','Between 18 and 64','65 and older']
      Age = random.choice(Age)

      Gender = ['Female','Male','Other']
      Gender = random.choice(Gender)

等等

我需要如下输出

       Location        Age          Gender
       Dubai           below 18     Female
       India           65 and older Male

。 . . .

【问题讨论】:

    标签: python python-3.x pandas dataframe random


    【解决方案1】:

    您可以为数据框中所需的行数创建一个 for 循环,然后生成字典列表。使用字典列表生成数据框。

    In [16]: for i in range(5):
        ...:     k={}
        ...:     loc = random.choice(Location)
        ...:     age = random.choice(Age)
        ...:     gen = random.choice(Gender)
        ...:     k = {'Location':loc,'Age':age, 'Gender':gen}
        ...:     list2.append(k)
        ...:
    
    In [17]: import pandas as pd
    
    In [18]: df = pd.DataFrame(list2)
    
    In [19]: df
    Out[19]:
                     Age Gender   Location
    0  Between 18 and 64  Other     Berlin
    1       65 and older  Other        USA
    2       65 and older   Male      Dubai
    3  Between 18 and 64   Male      Dubai
    4  Between 18 and 64   Male  Indonesia
    

    【讨论】:

      【解决方案2】:

      您可以使用np.random.choice 逐一创建每一列:

      df = pd.DataFrame()                                                                                                                                                                     
      N = 1000                                                                                                                                                                                
      df["Location"] = np.random.choice(Location, size=N)                                                                                                                                     
      df["Age"] = np.random.choice(Age, size=N)                                                                                                                                               
      df["Gender"] = np.random.choice(Gender, size=N)  
      

      或者使用列表推导来做到这一点:

      column_to_choice = {"Location": Location, "Age": Age, "Gender": Gender}
      
      df = pd.DataFrame(
          [np.random.choice(column_to_choice[c], 100) for c in column_to_choice]
      ).T
      
      df.columns = list(column_to_choice.keys())
      

      结果:

      >>> print(df.head())                                                                                                                                                                              
          Location                Age  Gender
      0      India       65 and older  Female
      1     Berlin  Between 18 and 64  Female
      2        USA  Between 18 and 64    Male
      3  Indonesia           Under 18    Male
      4      Dubai           Under 18   Other
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-05-31
        • 2016-02-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-22
        • 2022-11-22
        • 1970-01-01
        相关资源
        最近更新 更多