我正在寻找一种更有效的方法来执行检查,然后在必要时应用转换。
这是我当前的代码
for i in tqdm(df.index):
if df.loc[i,'WHP Total Acres'] > df.loc[i,'GIS_ACRES']:
conv_factor = (df.loc[i,'GIS_ACRES'])/(df.loc[i,'WHP Total Acres'])
df.loc[i,'Very Low'] = df.loc[i,'Very Low'] * conv_factor
df.loc[i,'Low'] = df.loc[i,'Low'] * conv_factor
df.loc[i,'Moderate'] = df.loc[i,'Moderate'] * conv_factor
df.loc[i,'High'] = df.loc[i,'High'] * conv_factor
df.loc[i,'Very High'] = df.loc[i,'Very High'] * conv_factor
else:
df.loc[i, 'WHP Total Acres'] = df.loc[i,'WHP Total Acres']
要遍历350K记录,这在我的计算机上大约需要2个小时。我确信必须有一种更有效的方法。预先谢谢你。
答案 0 :(得分:0)
df['conv_factor'] = 1
df.loc[df['WHP Total Acres'] > df['GIS_ACRES'],'conv_factor'] = df['GIS_ACRES']/df['WHP Total Acres']
df['Very Low'] = df['conv_factor'] * df['Very Low']
.
.
.
为conv_factor
创建一个帮助列,然后相乘即可得到结果。
只需使用loc
即可更改所需的值。