0

I'm looking To fill Nan population values for specific states/values for column State in my date Frame using jupiter notebooks.

Code:

DataGroupby = pd.DataFrame(Data.groupby(['State'])['Population'].mean())
DataGroupby = DataGroupby.reset_index()
DataGroupby.head(30)
DataChangePop = Data.loc[(Data['Population'].isnull() == True)]
Wisconsinmean = float((DataGroupby.loc[DataGroupby['State'] == 'Wisconsin','Population'].values)[0])
print(Wisconsinmean)
(Data.loc[Data['State'] == 'Wisconsin', 'Population']).fillna(Wisconsinmean  ,inplace = True)
for x in DataGroupby['State']:
Data.loc[Data['State'] == x,'Population'].fillna(value = (DataGroupby.loc[DataGroupby['State'] == x,'Population'].values)[0], axis = 0,inplace = True)       

I didn't try much but I expect the values that were Nan in Population to be the mean of the corresponding State i selected but the values are still Nan.

Shaido
  • 27,497
  • 23
  • 70
  • 73
Flyguy
  • 1
  • Does this answer your question? [Pandas: filling missing values by mean in each group](https://stackoverflow.com/questions/19966018/pandas-filling-missing-values-by-mean-in-each-group) – Shaido Aug 29 '23 at 02:59
  • It's hard to answer the question when we can't see how the data looks and the expected finish look. – Shane S Aug 29 '23 at 15:46

0 Answers0