转换列中的数据类型并替换NaN和其他值

时间:2020-02-03 18:21:08

标签: python pandas dataframe types nan

我在熊猫中有this dataframe,在某些单元格中具有NaN和“未知”值,并且所有列的数据类型均为“对象”。我要摆脱这种令人烦恼的值(禁止删除行),并将列类型更改为适当的类型。我已经尝试过以下代码:

import numpy as np
movies_converted = movies.copy()
movies_converted.replace(to_replace='Unknown', value=np.nan, regex=True)
movies_converted.fillna("", inplace=True)
movies_converted['Title']=movies_converted['Title'].astype('str')
movies_converted['US Gross']=pd.to_numeric(movies_converted['US Gross'],errors='coerce').astype('int') 
movies_converted['Worldwide Gross']=pd.to_numeric(movies_converted['Worldwide Gross'], errors='coerce').astype('int')
movies_converted['Production Budget']=pd.to_numeric(movies_converted['Production Budget']).astype('int')
movies_converted['Release Date']=pd.to_datetime(movies_converted['Release Date'],errors='coerce')
movies_converted['Distributor']=movies_converted['Distributor'].astype('str')
movies_converted['Source']=movies_converted['Source'].astype('str')
movies_converted['Major Genre']=movies_converted['Major Genre'].astype('str')
movies_converted['Creative Type']=movies_converted['Creative Type'].astype('str')
movies_converted['Director']=movies_converted['Director'].astype('str')
movies_converted['Rotten Tomatoes Rating']=pd.to_numeric(movies_converted['Rotten Tomatoes Rating'])
movies_converted['IMDB Rating']=movies_converted['IMDB Rating'].str.replace(',',".").astype('float', errors='ignore')
movies_converted['IMDB Votes']=pd.to_numeric(movies_converted['IMDB Votes'],errors='coerce')
movies_converted

我是Python的新手,所以我不知道自己在做错什么,但是我总是会遇到一些错误,这就是这个“ ValueError:无法将非有限值(NA或inf)转换为整数”。请提供一些有关最佳工作方式和错误的建议。

0 个答案:

没有答案