TypeError: only integer scalar arrays can be converted to a scalar index when use Pandas Fillna

喜夏-厌秋 提交于 2021-01-29 07:30:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!