将列值与数据框中的另一个值进行比较(天气数据预报)
compare column value with another value in a dataframe (weather data forecast)
我需要将我的列值(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']
没有数据,帮助总是很棘手,但你可以尝试适应这个:
# 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
我需要将我的列值(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']
没有数据,帮助总是很棘手,但你可以尝试适应这个:
# 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