[英]Assign value to pandas DataFrame with hierarchical index based on stacked condition
[英]Assign the random value based on satisfied condition accordingly to the matching index of pandas dataframe column
我正在尝试在 python 中填充数据。
代码如下。
代码的目的是在范围内选择一个随机值,并根据数据框列的大小填充熊猫的变量。 为了实现解决方案,如果满足条件,我在 for 循环中定义索引和项目变量,然后在新变量位置的循环变量的索引处添加随机值
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)
数据帧有 100K 数据。 当前问题: “解包的值太多”
我遇到了"Too many values to unpack" 的问题。 你能在这里指导我更正吗?
预期结果:根据满足的条件相应地分配随机值
最好的问候,加布
使用 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)
这是另一个解决方案,
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]))
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.