简体   繁体   中英

Error when converting dataframe object into int64

Good day. I am working on 2 dataframes that i will later be comparing, playersData & allStar . playersData['Year'] is type int64, while allStar is type object. i tried to convert the playersData['Year'] using the following code:

playersData['Year'] = playersData['Year'].astype(str).astype(int)

but it shows error saying:

ValueError: invalid literal for int() with base 10: 'nan'

the code I used is from the link: https://www.kite.com/python/answers/how-to-convert-a-pandas-dataframe-column-from-object-to-int-in-python

here is reference pics regarding types of my dataframes:

2个数据帧的dtypes

Try Dropping all the nan values from the dataset.

playersData.dropna(inplace=True)

You can either drop rows containing NaN values or replace them with a constant (In case there were few other columns containing valuable info, dropping rows might not be a good option).

  1. If you want to drop playersData.dropna(inplace=True) or playersData = playersData.dropna()

  2. Replacing with a constant (Ex: 0) playersData['Year'].fillna(0, inplace=True) or playersData['Year'] = playersData['Year'].fillna(0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM