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?
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'
The manner of calling the function involves using the module and then passing in the column of the DataFrame
you want to modify, like so:
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_Annual
with values, perform the following and see the result:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With