pandas 谁能解释这个错误 [AttributeError: 'DataFrame' object has no attribute 'to_numeric']

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38515783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:38:46  来源:igfitidea点击:

Can anyone explain this error [AttributeError: 'DataFrame' object has no attribute 'to_numeric']

pythonpandas

提问by Mark

I'm tryting to change the salaries to the intergers so I can then do some analysis and make a chart of their price per pitch. When I try to do this it says that the dataframe doesnt have the attribute to_numeric. I got the code of the API DOCs so I was wondering what is happening. Is it a list of DataFrames or something. Should I change the number signs out of it?

我正在尝试将工资更改为整数,以便我可以进行一些分析并制作每个间距的价格图表。当我尝试这样做时,它说数据框没有 to_numeric 属性。我得到了 API DOC 的代码,所以我想知道发生了什么。它是一个数据帧列表还是什么。我应该改变它的数字标志吗?

import pandas as pd
import pandas_datareader.data as web

players = pd.read_html('http://www.usatoday.com/sports/mlb/salaries/2013/player/p/')


df1 = pd.DataFrame(players[0])
df1.drop(df1.columns[[0,3,4, 5, 6]], axis=1, inplace=True)
df1.columns = ['Player', 'Team', 'Avg_Annual']
#print (df1.head(10))

p2 = pd.read_html('http://www.sportingcharts.com/mlb/stats/pitching-pitch-count-leaders/2013/')


df2 = pd.DataFrame(p2[0])

df2.drop(df2.columns[[0,2, 3]], axis=1, inplace=True)



#print (df2.head(10))

df1.set_index ('Player')
df2.set_index('Player')




df3 = pd.merge(df1, df2, on='Player')

df3.set_index('Player', inplace=True)
df3.columns = ['Team', 'Avg_Annual', 'Pitch_Count']
print (df3.head())

df3.to_numeric(Avg_Annual)
values = (df3.Avg_Annual) - (df3.Pitch_Count)

print (values.head())

Which gives error:

这给出了错误:

Traceback (most recent call last): File "/home/mdz5032/PMLB.py", line 38, in df3.to_numeric(Avg_Annual) File "/usr/local/lib/python3.4/dist-packages/pandas/core/generic.py", line 2672, in getattr return object.getattribute(self, name) AttributeError: 'DataFrame' object has no attribute 'to_numeric'

回溯(最近一次调用):文件“/home/mdz5032/PMLB.py”,第 38 行,在 df3.to_numeric(Avg_Annual) 文件“/usr/local/lib/python3.4/dist-packages/pandas/core /generic.py”,第 2672 行,在 getattr 返回对象中。getattribute(self, name) AttributeError: 'DataFrame' 对象没有属性 'to_numeric'

回答by Jossie Calderon

The manner of calling the function involves using the module and then passing in the column of the DataFrameyou want to modify, like so:

调用函数的方式是使用模块,然后传入DataFrame要修改的列,如下所示:

pd.to_numeric(df3.Avg_Annual)

You'll get another error because the module can't convert dollar signs and commas to numeric. Try this:

您将收到另一个错误,因为该模块无法将美元符号和逗号转换为数字。尝试这个:

values = []

for i in range(0, len(df3.Avg_Annual)):
    values.append(int(df3.Avg_Annual[i][2:].replace(',','')) - df3.Pitch_Count[i])

If you want to replace df3.Avg_Annualwith values, perform the following and see the result:

如果要替换df3.Avg_Annual为值,请执行以下操作并查看结果:

for i in range(0, len(df3.Avg_Annual)):
    df3.Avg_Annual[i] = (int(df3.Avg_Annual[i][2:].replace(',','')) - df3.Pitch_Count[i])
print (df3.head())

If you want to re-add the format, it's easy.

如果您想重新添加格式,这很容易。