Is there a shorter, more compact alternative to loc multiple variables into separate columns in a loop?
The code that I am using now is looping through each polygon by index, then finding which point (from point file) is located in which polygon by ID. Then find some metrics or any other variable by equation and loc in new columns.
So, I was thinking if there is other way to loc multiple variables in separate columns to avoid great list of polygon.loc[n, 'column name'] = some variable
.
for n in polygon.index:
points_in_poly = points[points['ID'] == n]
# then equation to find some variables in each row
maxv = points_in_poly['Height'].max()
mean = points_in_poly['Height'].mean()
median = points_in_poly['Height'].median()
std = points_in_poly['Height'].std()
half_std = std / 2
max1 = maxv * std 15
# then by doing this I'm get each row corresponding value
polygon.loc[n, 'max'] = maxv
polygon.loc[n, 'mean'] = mean
polygon.loc[n, 'median'] = median
polygon.loc[n, 'std'] = std
polygon.loc[n, 'half_std'] = half_std
polygon.loc[n, 'max1'] = max1
CodePudding user response:
You could update your rows by using a dictionary like so:
polygon.loc[n] = {'max': maxv, 'mean': mean, 'median': median, 'std': std, 'half_std': half_std, 'max1': max1}
If there are other rows with values you don't want to overwrite you should add them to the dictionary. Something like this would work:
polygon_data = polygon.loc[n].to_dict()
polygon_data.update({'max': maxv, 'mean': mean, 'median': median, 'std': std, 'half_std': half_std, 'max1': max1})
polygon.loc[n] = polygon_data