Python 在调用数据类型对象的平均值时,DataFrame TypeError必须为str not int
我正在使用Pandas和python(3)处理一个数据集,其中我需要去除空值,因此我尝试采用该特定列的平均值来填充空值,但得到以下错误: TypeError:必须是str,而不是int 当我在数据帧上调用Python 在调用数据类型对象的平均值时,DataFrame TypeError必须为str not int,python,python-3.x,pandas,data-science,mean,Python,Python 3.x,Pandas,Data Science,Mean,我正在使用Pandas和python(3)处理一个数据集,其中我需要去除空值,因此我尝试采用该特定列的平均值来填充空值,但得到以下错误: TypeError:必须是str,而不是int 当我在数据帧上调用.info()时,我得到了它的数据类型对象: 对象 以下是数据集的链接,以防您需要更多信息: 那么,如何获得数据框列中object和float64类型值的平均值呢 提前谢谢 您可以在列上使用内置的.fillna()方法 所以你可以这样做 df['column_of_interest'] = p
.info()
时,我得到了它的数据类型对象:
对象
以下是数据集的链接,以防您需要更多信息:
那么,如何获得数据框列中object
和float64
类型值的平均值呢
提前谢谢 您可以在列上使用内置的
.fillna()
方法
所以你可以这样做
df['column_of_interest'] = pd.to_numeric(df['column_of_interest'])
my_mean = df['column_of_interest'].dropna().mean()
df['column_of_interest'].fillna(my_mean)
您可能需要设置
inplace=True
,否则只需使用df['column\u of_interest']=df['column\u of_interest'].fillna(我的意思是)
,如果它没有自动填充NAN的话。就像导入csv文件时,使用特定值的示例数据为null
快速修复
df=pd.read_csv('your.csv',na_values = [ '#N/A', 'NA', 'NULL', 'NaN', 'n/a', 'nan', 'null'])
另一个补丁
for x in ['wordsintitle', 'imdbrating', 'ratingcount', 'duration', 'year']:
df[x]=pd.to_numeric(df[x],errors='coerce')
给我们看一些样本数据?对象还表示您的列是混合类型,与字符串类似。因此,哪些列会引发问题?我已添加到数据集的链接。您使用的是fillna?以下是具有空值的列:
wordsintitle
,imdbrating
,ratingcount
,duration
,year
但我认为它会粘在第一列上。它返回:ValueError:无法将字符串转换为float:
您希望该列是数字吗?如果是这样,可能是其中的某些值是字符串而不是浮点数,在这种情况下,您可以尝试df['column\u of\u interest']=pd.to\u numeric(df['column\u of\u interest'])
,然后尝试上面的代码。