【问题标题】:How to add random values in a pandas dataframe with specific set of conditions如何在具有特定条件的熊猫数据框中添加随机值
【发布时间】:2019-10-17 06:36:13
【问题描述】:

我正在尝试在预期范围内有条件地添加随机值。

数据 =

LOT NO  QTY(Kgs)    % PICK      11C     12C     13C      14C    15C     16C
H19       312        6.22                       
H20       936        18.67                      
H21       989        19.72                      
H22       559        11.15                      
H23       639        12.74                      
H24       736        14.68                      
H25       843        16.81

其中 11c 到 16C 是具有空值 (nans) 或零的列。

我想用一组条件添加或替换随机值(int & float)

  1. 11C 列中值的平均总和应在 9-12.5 之间。
  2. 12C 列中值的平均总和应在 43-47 之间。 3. 4. 其他条件如下
11C    |    12C   |     13C  |   14C    |     15C    |   16C
--------------------------------------------------------------
9-12.5 |  45+/-2  |  205-230 |  5.0-6.0 |  <1000     |  <1500
---------------------------------------------------------------

我的预期输出:

LOT NO    QTY (Kgs)   % PICK    11C     12C     13C  14C    15C 16C
H19       312          6.22     10.50   45.30   247  5.46   53  430
H20       936          18.67    10.38   48.48   265  5.64   67  280
H21       989          19.72    10.62   44.38   264  5.66   73  325
H22       559          11.15    10.97   43.52   226  5.54   62  365
H23       639          12.74    10.89   46.53   205  5.71   84  345
H24       736          14.68    11.09   43.76   165  5.62   93  230
H25       843          16.81    11.01   39.96   137  5.68   95  160

我该怎么做?

【问题讨论】:

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


    【解决方案1】:

    更新

    好吧,假设df2 是您的初始DataFrame。这是一个使用dictionary 条件的示例:

    import pandas as pd
    import numpy as np
    
    df = pd.DataFrame()
    df2 = pd.DataFrame([[1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6]],
          columns=['11C', '12C', '13C', '14C', '15C', '16C'])
    
    
    def n(_min, _max=None, rows=7, getint=None):
        if getint == 'AVG':
            return [round(x, 2) for x in _min + (_max - _min) * np.random.rand(rows)]
        _min = int(_min / rows)
        return np.random.choice(_min, rows)
    
    
    conditions = {'11C': n(9, 12.5, getint='AVG'), '12C': n(43, 47, getint='AVG'), '13C': n(205, 230, getint='AVG'),
                  '14C': n(5, 6, getint='AVG'), '15C': n(1000, None), '16C': n(1500, None)}
    for key, val in conditions.items():
        df[key] = val
    
    print(df)
    df2.update(df)
    

    df2.update(df) 将更新 dfdf2 中的所有键,但请确保它们具有相同的行数,而 update() 将更新现有的行数。

    结果

         11C    12C     13C   14C  15C  16C
    0  11.37  43.43  223.43  5.66  126  181
    1  11.67  45.08  217.87  5.80   91   16
    2   9.39  43.95  218.13  5.24   69   71
    3  12.23  44.74  215.62  5.87   11  129
    4  12.42  45.86  209.75  5.05    5  132
    5   9.49  45.28  227.34  5.83    2    4
    6   9.35  45.08  218.40  5.34  129   48
    
    

    【讨论】:

    • 1.如何将值替换为existing dataframe. 2. 列13C 必须是int 3. 无论如何要将小数点四舍五入到np.random.rand 中的两位
    • @Krishna 2 和 3 已在上面更新。现在您要替换哪个数据框?
    • 进一步澄清:The avg sum of the values in columns should be in the specified range。但是使用代码会给出范围内的随机值。假设rows=7 的数量。每列的平均值应在问题中提到的范围内。
    • 这个过程真的很漫长,所以+1
    • @Krishna 你让我有点困惑,我希望这就是你要找的。​​span>
    【解决方案2】:

    你可以像下面那样使用 np.random 函数

    df = pd.DataFrame()
    n_rows = 10
    df["11C"] = 9+ (12.5-9)*np.random.rand(n_rows)
    df["12C"] = 43+ (47-43)*np.random.rand(n_rows)
    df["13C"] = 205+ (330-205)*np.random.rand(n_rows)
    df["14C"] = 5+ (5-6)*np.random.rand(n_rows)
    
    df["15C"] = np.random.choice(1000, n_rows)
    df["15C"] = np.random.choice(1500, n_rows)
    df
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-14
      • 2019-12-21
      • 1970-01-01
      • 2019-10-04
      • 2018-03-04
      • 2017-01-12
      • 2018-09-06
      • 2015-03-29
      相关资源
      最近更新 更多