我有一个包含5列的数据帧,其中需要聚合的列是一个字符串,并且具有NaN值。 我尝试将nan值替换为0,然后将列转换为数字,但在执行gruopby和aggregate函数时仍然得到“DataError:No numeric types to aggregate”。你知道吗
df.head()
df['Profit (in millions)']= df['Profit(in millions)'].str.replace('N.A', '0')
pd.to_numeric(df['Profit (in millions)'], errors ='ignore')
df_new = df.groupby('Year')['Profit (in millions)'].median()
df_new.head(7)
有两个问题,您忘记了赋值,如果
errors ='ignore'
和列中至少有一个不可赋值的值,它将返回没有更改的列:^{} :
所以使用
errors ='coerce'
并将输出返回到列:相关问题 更多 >
编程相关推荐