我正在嘗試使用以下函數替換 NAN 值,但我得到的索引超出界限錯誤。這是我的示例數據框。它有列(日期、中心名稱、商品名稱、價格、年份)。我正在嘗試根據相應的年份和中心名稱列使用價格的 MODE() 替換價格列缺失值。例如,對于 1997 年和 Centre_Name='SHIMLA',我使用下面的代碼來替換 Price 列的缺失值并且它正在工作。data.loc[(data['Year']==1997)&(data['Centre_Name']=='SHIMLA')&(data['Price'].isnull()),'Price']=data.loc[(data['Year']==1997)&(data['Centre_Name']=='SHIMLA'),'Price'].mode()[0]但是下面的功能不起作用。請幫忙year_list=list(data['Year'].unique())for each_year in year_list: city_list=list(data[data['Year']==each_year]['Centre_Name'].unique()) for each_city in city_list: data.loc[(data['Year']==each_year)&(data['Centre_Name']==each_city)&(data['Price'].isnull()),'Price']=data.loc[(data['Year']==each_year)&(data['Centre_Name']==each_city),'Price'].mode()[0]
添加回答
舉報
0/150
提交
取消