【问题标题】:Assign the random value based on satisfied condition accordingly to the matching index of pandas dataframe column根据熊猫数据框列的匹配索引,根据满足条件分配随机值
【发布时间】:2020-11-03 13:03:23
【问题描述】:

我正在尝试在 python 中填充数据。

代码如下。

代码的目的是在范围内选择一个随机值,并根据数据框列的大小填充 panda 的变量。 为了实现解决方案,我在 for 循环中定义了 index 和 item 变量 如果条件满足,则在新变量位置循环变量的索引处添加随机值

import scipy.stats as stats
for index , item in df['RangeCategory']:
    if (item ==  'RC1' ):
        upperLim = 0.5
        lowerLim = 50.4
        df["ItemFlagged"][index] =  random.uniform(upperLim, lowerLim)
    elif (item ==  'RC2' ):
        upperLim = 50
        lowerLim = 250.4
        df["ItemFlagged"][index] =  random.uniform(upperLim, lowerLim)
    elif (item ==  'RC3' ):
        upperLim = 250
        lowerLim = 300.4
        df["ItemFlagged"][index] =  random.uniform(upperLim, lowerLim)
    elif (item ==  'RC4' ):
        upperLim = 300
        lowerLim = 500.4
        df["ItemFlagged"][index] =  random.uniform(upperLim, lowerLim)
    elif (item ==  'RC5' ):
        upperLim = 500
        lowerLim = 900.4
        df["ItemFlagged"][index] =  random.uniform(upperLim, lowerLim)
    elif (item ==  'RC6' ):
        upperLim = 900
        lowerLim = 1500.4
        df["ItemFlagged"][index] =  random.uniform(upperLim, lowerLim)
    elif (item ==  'RC7' ):
        upperLim = 1500
        lowerLim = 3000.4
        df["ItemFlagged"][index] =  random.uniform(upperLim, lowerLim)
    elif (item ==  'RC8' ):
        upperLim = 3000
        lowerLim = 5000.4
        df["ItemFlagged"][index] =  random.uniform(upperLim, lowerLim)
    elif (item ==  'RC9' ):
        upperLim = 5001
        lowerLim = 9000.4
        df["ItemFlagged"][index] =  random.uniform(upperLim, lowerLim)
    elif (item ==  'RC10' ):
        upperLim = 9001
        lowerLim = 10000.4
        df["ItemFlagged"][index] =  random.uniform(upperLim, lowerLim)
    elif (item ==  'RC11' ):
        upperLim = 10001
        lowerLim = 30000
        df["ItemFlagged"][index] =  random.uniform(upperLim, lowerLim)
    elif (item ==  'RC12' ):
        upperLim = 31000
        lowerLim = 50000
        df["ItemFlagged"][index] =  random.uniform(upperLim, lowerLim)

Dataframe 有 100K 数据。 当前问题:“要解压的值太多”

我遇到了“太多值无法解压”的问题。你能指导我在这里更正吗?

预期结果: 相应地根据满足的条件分配随机值

最好的问候, 加布

【问题讨论】:

    标签: python python-3.x pandas random


    【解决方案1】:

    使用loc函数赋值:

    for index , row in df.iterrows():
        if row['RangeCategory'] == 'RC1':
            upperLim = 0.5
            lowerLim = 50.4
            df.loc[index,"ItemFlagged"] = random.uniform(upperLim, lowerLim)
    

    【讨论】:

      【解决方案2】:

      这是另一种解决方案,

      import random
      
      # define a look up RangeCategory
      range_lookup = {"RC1": [0.5, 50.4], "RC2": [50, 250.4]...}
      
      df['ItemFlagged'] = (
          df['RangeCategory'].apply(lambda x: random.uniform(*range_lookup[x]))
      )
      

      【讨论】:

        猜你喜欢
        • 2021-07-24
        • 2017-11-03
        • 2020-12-11
        • 2018-05-24
        • 2023-02-21
        • 2021-10-16
        • 1970-01-01
        • 1970-01-01
        • 2023-02-02
        相关资源
        最近更新 更多