我需要將我的列值(113 839 個值)與類別(位置)(44 個值)的平均值(降雨量)進行比較。如果它高于我的平均值,則應替換為平均值。我的 foreach 不起作用:
df_rainfall = pd.DataFrame(weather_train_data_total.groupby(['Location'])['Rainfall'].mean())
for column in weather_train_data_total[['Location']]:
result = weather_train_data_total[column]
print(result)
if result.equals(df_rainfall['Location']):
result = df_rainfall['Rainfall']

uj5u.com熱心網友回復:
沒有資料,幫助總是很棘手,但您可以嘗試適應這一點:
# calculate and assign the average value for each group
df["mean_val"] = df.groupby("Location")["Rainfall"].transform("mean")
# identify rows in which the value is above the average
relevant_rows = df["mean_val"] < df["Rainfall"]
# replace these values with their corresponding average
df.loc[relevant_rows, ["Rainfall"]] = df.loc[relevant_rows, ["mean_val"]]["mean_val"]
df
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/369013.html
上一篇:如何將串列添加到資料框?
