+ 不支持的操作数类型:'int' 和 'str' 使用 Pandas 表示

Gab*_*rme 7 python dataframe pandas

当我尝试获取数据框列之一的平均值时,它显示错误:

TypeError: unsupported operand type(s) for +: 'int' and 'str'
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

import pandas as pd

import numpy as np

url = "https://archive.ics.uci.edu/ml/machine-learning-databases/autos/imports-85.data"

df = pd.read_csv(url, header = None, )

headers = ["symboling","normalized-losses","make","fuel-type","aspiration","num-of-doors","body-style","drive-wheels","engine-location","wheel-base","lenght","width","height","curb-weight","engine-type","num-of-cylinders","engine-size","fuel-system","bore","stroke","compression-ratio","horsepower","peak-rpm","city-mpg","highway-mpg","price"]

df.columns = headers

df.replace('?',np.nan, inplace=True)

mean_val = df['normalized-losses'].mean()

print(mean_val)
Run Code Online (Sandbox Code Playgroud)

Nat*_*iel 7

您需要使用 将列数据类型转换为数字pd.to_numeric()。如果您使用该选项,errors='coerce'那么它会自动用NaN.

mean_val = pd.to_numeric(df['normalized-losses'], errors='coerce').mean()

print(mean_val)

> 122.0
Run Code Online (Sandbox Code Playgroud)