Search code examples
pythonpandasmachine-learningdata-analysismissing-data

Facing Index out of bounds Error when replacing NaNs in a column using a function in Pandas


I am trying to replace NAN values using below function,but i am getting Index out of Bound Error.This is my sample Dataframe. It has columns(Date,Centre_Name,Commodity_Name,Price,Year).I am trying to replace Price column missing values using MODE() of Price based on corresponding Year and Centre_Name columns.

For example for year 1997 and Centre_Name='SHIMLA' I am using below code to replace missing values of Price column and it is working.

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]

But below function is not working.Please help

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]

Solution

  • in the second script replace data.iloc with data.loc