问题
Update : It seems due to .loc ,if i uses the original df from pd.read_excel, it is fine.
I have a dataframe with Dtypes as follows.
This is csv for the dataframe : CSV File
Date datetime64[ns] Amout float64 Currency object ID object
I used the following code to replace NaT, NaN
a=np.datetime64('2000-01-01')
values={'Date':a,'Amount':0,'Currency':'0','ID':'0'}
df.fillna(value=values,inplace=True)
However, I got the error : TypeError: only integer scalar arrays can be converted to a scalar index.
I also tried to fillna with each column and I saw no error message but the Nan and Nat still remain unchanged.
a=np.datetime64('2000-01-01')
df[['Date']].fillna(a,inplace=True)
df[['Amount']].fillna(0,inplace=True)
df[['Currency']].fillna('0',inplace=True)
df[['ID']].fillna('0',inplace=True)
It seems very strange to me since I have use fillna successfully many times. Please give me an advice. Thank you so much.
回答1:
I think there are strings NaN
in Currency
and Id
columns, so use:
df = df.replace({'Currency': {'NaN': '0'}, 'ID':{'NaN': '0'}})
来源:https://stackoverflow.com/questions/64154190/typeerror-only-integer-scalar-arrays-can-be-converted-to-a-scalar-index-when-us