'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.**
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
Use at
here:
df.at[22286, 'total_cases'] = value
print(df.loc[22286]['total_cases'])
5300.0
import pandas as pd
df = pd.read_csv('data.csv')
df.loc[17,'Calories']=679
print(df.to_string())
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.**
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
Use at
here:
df.at[22286, 'total_cases'] = value
print(df.loc[22286]['total_cases'])
5300.0
import pandas as pd
df = pd.read_csv('data.csv')
df.loc[17,'Calories']=679
print(df.to_string())