'numpy.float64' object has no attribute 'fillna' when filling NaN

Question:

I am trying to fill one missing value with an exact number.
I have a covid data set and in Honk Kong there are some columns which have missing values, however I just one NaN value to be filled.

df = pd.read_csv("https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/owid-covid-data.csv")

df= df[df['date']=='2020-10-27']
df

df[df.location=='Hong Kong']  

value = 5300

##22286 --> it is the index where HK 

df.loc[22286]['total_cases'] = df.loc[22286]['total_cases'].fillna(value)

I get the following error:

AttributeError: ‘numpy.float64’ object has no attribute ‘fillna’**

Then I tried to convert the value into float:

value= float(value)

However I get the following message :

A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
"""Entry point for launching an IPython kernel.**

Asked By: AlexGo

||

Answers:

fillna() is an array function that is mapping the items,
you accessing a single value and not a numpy/pandas array…:

instead do

df.loc[22286,'total_cases'] = value
Answered By: adir abargil

Use at here:

df.at[22286, 'total_cases'] = value
print(df.loc[22286]['total_cases'])

5300.0
Answered By: NYC Coder
import pandas as pd

df = pd.read_csv('data.csv')

df.loc[17,'Calories']=679

print(df.to_string())
Answered By: YASH PATEL
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.