【发布时间】:2016-11-25 17:28:21
【问题描述】:
我正在尝试将薪水更改为整数,这样我就可以进行一些分析并制作每个球场的价格图表。当我尝试这样做时,它说数据框没有属性 to_numeric。我得到了 API DOC 的代码,所以我想知道发生了什么。它是 DataFrames 列表还是其他东西。我应该把它的数字符号改掉吗?
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())
这给出了错误:
Traceback(最近一次调用最后一次):文件“/home/mdz5032/PMLB.py”,行 38、在df3.to_numeric(Avg_Annual)文件中 “/usr/local/lib/python3.4/dist-packages/pandas/core/generic.py”,行 2672,在 getattr 返回对象中。getattribute(自我,姓名) AttributeError: 'DataFrame' 对象没有属性 'to_numeric'
【问题讨论】:
-
请在代码中打印错误。
-
Traceback(最近一次调用最后):文件“/home/mdz5032/PMLB.py”,第 38 行,在
df3.to_numeric(Avg_Annual) 文件“/usr/local/lib/ python3.4/dist-packages/pandas/core/generic.py",第 2672 行,在 getattr 中返回 object.__getattribute__(self, name) AttributeError: 'DataFrame' object has no attribute 'to_numeric' -
我已经更新了我的答案。它适用于我的情况,在你的情况下尝试一下。
-
解决方案有帮助吗?