我有两个不同的表df_postErup和df_preErup。我想从df_postErup中获取某个月份的值,然后从df_preErup中扣除相应的平均值。 (即,对于month = 3,从df_postErup中获得值0.394,然后从df_preErup中获得该月= 3的均值= 0.669。然后计算差值并将其存储在名为“ delta”的新列中0.394-0.669)
我编写了以下代码,但是它给了我Nan值,
for i in range (1,13):
df_postErup['delta'] = (df_postErup.loc[df_postErup['months'] == i, 'values']) - (df_preErup.loc[df_preErup['months'] == i, 'mean_values'])
答案 0 :(得分:0)
设法解决了这个问题,但是如果有人有更好的答案,请发布
#Create a New empty column in df_postErup which will store the mean values for each month from df_pre
df_postErup['mean ndvi pre'] = ""
for i in range(0,12):
df_postErup.loc[df_postErup['months'] == (i + 1), ['mean ndvi pre']] = df_preErup.iloc[i,1]
#Calculate the difference, delta
df_postErup['delta'] = df_postErup['ndvi'] - df_postErup['mean ndvi pre']