Pandas.dataframe.fillna - Typeerror: Only Integer Scalar Arrays Can Be Converted To A Scalar Index
I am trying to do this from the official pandas documentation. pandas.DataFrame.fillna So Basicly filling up the NaN values in the df dataframe's 'myc' column with values of 1. DA
Solution 1:
Something weird is going on in your code, because:
- the replacement of NaN should occur only in myc column,
- but your result contains replaced values also e.g. in C column and NaN are replaced there with 2.
Run just the below code (separated from your code):
import pandas as pd
import io
txt = '''myc,B,C,D
NaN,2.0,NaN,0
3.0,4.0,NaN,1
NaN,NaN,NaN,5
NaN,3.0,NaN,4'''
df = pd.read_csv(io.StringIO(txt))
result = df.fillna(value={'myc': 1})
The result should be:
myc B C D
01.02.0NaN013.04.0NaN121.0NaNNaN531.03.0NaN4
If you get the same result, then apparently there is something wrong with your code, but in some other place (outside the piece of code that you presented).
Another detail to change is that values is an attribute of Pandas and you should not use variables with the same names.
Solution 2:
Simple Solution
- Export dataframe to csv:
df.to_csv(r'somefilename.csv', index=False)
- Load back the saem data to a DataFrame:
df1 = pd.read_csv("r'somefilename.csv")
Post a Comment for "Pandas.dataframe.fillna - Typeerror: Only Integer Scalar Arrays Can Be Converted To A Scalar Index"