0

I am trying to replace some nan values in a few columns using a calculation from other columns. ie.

nancolumn = column1.value + column2.value

My first attempt didn't work, ie there is still nan values

indecies = list(list(map(tuple, np.where(np.isnan(df['nancolumn']))))[0])
newValue = df.iloc[indecies]['column1'] + df.iloc[indecies ]['column2']
df.iloc[indecies]['nancolumn'] = newValue

I then found a specific index that i wanted to replace, 1805, and tried just replacing this data point value with 1.0. The result is still a nan

df.iloc[1805]['nancolumn'] = 1.0

I tried using fillna(), and isnan()

df[np.isnan(df)]=1

I get this error for the isnan() attempt:

TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

df.iloc[1805]['nancolumn'].dtype
dtype('float64')

I know im missing something simple, but i can't figure it out.

Can someone please help?

theotheraussie
  • 495
  • 1
  • 4
  • 14
  • I think this will help https://stackoverflow.com/questions/13295735/how-can-i-replace-all-the-nan-values-with-zeros-in-a-column-of-a-pandas-datafra – S.Harish Dec 06 '18 at 11:02

1 Answers1

0

I found out that its best to use reference the column first and then the index, like below

df['nancolumn'].iloc[1805] = 1.0

Although, i still don't really understand the difference. If any one has a explenation, that would be helpful.

theotheraussie
  • 495
  • 1
  • 4
  • 14