我正在努力将薪水改为整数,这样我就可以做一些分析并制作每个球场的价格图表。当我尝试这样做时,它表示数据框没有属性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())
答案 0 :(得分:5)
调用函数的方式包括使用模块然后传入要修改的DataFrame
列,如下所示:
pd.to_numeric(df3.Avg_Annual)
您将收到另一个错误,因为该模块无法将美元符号和逗号转换为数字。试试这个:
values = []
for i in range(0, len(df3.Avg_Annual)):
values.append(int(df3.Avg_Annual[i][2:].replace(',','')) - df3.Pitch_Count[i])
如果要将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())
如果您想重新添加格式,很容易。