gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 14:10:33 28 4
gpt4 key购买 nike

我正在尝试将薪水更改为整数,这样我就可以进行一些分析并绘制出他们每次投球的价格图表。当我尝试这样做时,它说数据框没有属性 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 (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'

最佳答案

调用函数的方式是使用模块,然后传入要修改的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())

如果你想重新添加格式,很简单。

关于python - 谁能解释这个错误 [AttributeError : 'DataFrame' object has no attribute 'to_numeric' ],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38515783/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com