【问题标题】:Generating random data iusing multiindex grouped_by dataframe object in Python在 Python 中使用 multiindex grouped_by 数据框对象生成随机数据
【发布时间】:2019-02-27 15:21:06
【问题描述】:

下表包含有关每个领导者和费用类型的费用的汇总统计信息。我将稳定存储在 python 中作为多索引数据框对象。我的目标是使用每个类别下的平均值和标准差为每个领导者和费用类型生成随机数据(运行下面的代码 sn-p 以获取表格)。有一个“计数”列,表示我想为每个 Leader-Expense_Type 组合生成多少个随机数。我提出了广泛而低效的循环结构,似乎无法正确完成工作。我应该如何解决这个问题?

注意:这只是数据的一个样本。还有更多的领导者拥有同样多的费用类型。

<table border="1" class="dataframe">  <thead>    <tr>      <th></th>      <th></th>      <th colspan="3" halign="left">Expense_Amount</th>    </tr>    <tr>      <th></th>      <th></th>      <th>mean</th>      <th>std</th>      <th>count</th>    </tr>    <tr>      <th>Leader</th>      <th>Expense_Type</th>      <th></th>      <th></th>      <th></th>    </tr>  </thead>  <tbody>    <tr>      <th rowspan="7" valign="top">Leader1</th>      <th>Airfare</th>      <td>1979.684219</td>      <td>2731.629767</td>      <td>1358</td>    </tr>    <tr>      <th>Booking Fees</th>      <td>118.994538</td>      <td>270.007390</td>      <td>1179</td>    </tr>    <tr>      <th>Conference/Seminars</th>      <td>1553.830923</td>      <td>1319.295946</td>      <td>65</td>    </tr>    <tr>      <th>Hotel</th>      <td>1656.643658</td>      <td>2104.721093</td>      <td>1405</td>    </tr>    <tr>      <th>Meals</th>      <td>435.665122</td>      <td>676.705857</td>      <td>1476</td>    </tr>    <tr>      <th>Mileage</th>      <td>213.785046</td>      <td>284.908031</td>      <td>979</td>    </tr>    <tr>      <th>Taxi/Uber</th>      <td>308.530724</td>      <td>380.288964</td>      <td>1422</td>    </tr>    <tr>      <th rowspan="7" valign="top">Leader2</th>      <th>Airfare</th>      <td>1730.196911</td>      <td>2334.688155</td>      <td>628</td>    </tr>    <tr>      <th>Booking Fees</th>      <td>112.020556</td>      <td>573.407269</td>      <td>576</td>    </tr>    <tr>      <th>Conference/Seminars</th>      <td>1647.576500</td>      <td>1154.320584</td>      <td>80</td>    </tr>    <tr>      <th>Hotel</th>      <td>1693.080356</td>      <td>1953.552474</td>      <td>618</td>    </tr>    <tr>      <th>Meals</th>      <td>574.228548</td>      <td>844.997595</td>      <td>620</td>    </tr>    <tr>      <th>Mileage</th>      <td>215.898798</td>      <td>291.231331</td>      <td>466</td>    </tr>    <tr>      <th>Taxi/Uber</th>      <td>298.655852</td>      <td>340.926518</td>      <td>569</td>    </tr>  </tbody></table>

【问题讨论】:

  • 我不确定有没有办法避免循环,每个分布都是独一无二的。我认为它没有必要成为“广泛的?”循环,您只需要使用三列给出的参数调用np.random.normal()(假设正常),因此您的代码可以非常简洁。也许发布您尝试过的内容,以便我们了解如何改进它。那么我们至少有一些东西可以作为基准。
  • 感谢@ALollz,我在下面发布了我的解决方案。

标签: python pandas numpy pandas-groupby


【解决方案1】:

您可以将df.apply(your_function, axis=1) 用于

def your_function(df):
    mean = df['mean']
    std = df['std']
    result = mean  # Replace with your number generator
    return result

有关更广泛的描述,请参阅此答案:How to apply a function to two columns of Pandas dataframe

【讨论】:

  • 这不起作用,因为它是一个多索引数据框。我不能用 df["mean"] (或 df["std"]) 对它进行切片。
【解决方案2】:

这是我的解决方案:

# Dictionary to hold generated data
rand_expenses_dict = {}

# Loop over each unique leader
for leader in agg_data.index.get_level_values("Leader").unique():

# Loop over each unique expense type
for expense_type in agg_data.index.get_level_values("Expense_Type").unique():

    # Not al leaders have all expense types
    # The exception handling method will ignore expense types
    # That do not correspond to a leader
    try:

        # Generate random numbers
        rand = (np.round(
                        np.random.normal(
                            loc=agg_data.loc[leader, expense_type][0],
                            scale = agg_data.loc[leader, expense_type][1],
                            size  = int(agg_data.loc[leader, expense_type][2])
                        ),2))

        # Add random numbers to data dictionaty
        rand_expenses_dict[(leader,expense_type)] = rand

    # If it finds an error, go to the next expense
    except:
        pass

【讨论】:

    猜你喜欢
    • 2015-01-22
    • 1970-01-01
    • 1970-01-01
    • 2017-03-30
    • 2014-10-24
    • 1970-01-01
    • 2015-07-26
    • 2013-11-15
    • 1970-01-01
    相关资源
    最近更新 更多