我在某些盆地有一些探头的高度数据。零高度值是虚假的,我想用同一盆地中探头的平均高度值代替它们。import pandas as pdindex = [0,1,2,3,4,5]s = pd.Series([0,2,2,0,1,6],index= index) #height valuest = pd.Series(['A','A','A','B','B','B'],index= index) #basins' namesdf = pd.concat([s,t], axis=1, keys=['Height','Basin'])print(df) Height Basin0 0 A1 2 A2 2 A3 0 B4 1 B5 6 B我首先创建一个 DataFrame 来存储盆地中的平均高度:#find height avergage in same basinbound_df = df[df['Height']>0]mean_height_df = bound_df.groupby(['Basin'])['Height'].mean()print(mean_height_df)BasinA 2.0B 3.5我尝试用相应盆地的平均值替换零值:#substitute zeros w/ the average valuedf.loc[df['Height']<=0, 'Height'] = mean_height_df.loc[mean_height_df['Basin'],'Height']但这会引发我不明白的错误:文件“pandas/_libs/hashtable_class_helper.pxi”,第 1218 行,在 pandas._libs.hashtable.PyObjectHashTable.get_itemKeyError: '盆地'这是什么意思?是切片问题吗?有没有替代方法?
添加回答
举报
0/150
提交
取消