TypeError: 'type' 对象在 Pandas DataFrame 中没有属性 '__getitem__'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24320862/
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
TypeError: 'type' object has no attribute '__getitem__' in pandas DataFrame
提问by ethanluo
I was trying to analyse the play-by-play data of a basketball team What I did was to read a csv file into a DataFrame object.
我试图分析一个篮球队的比赛数据我所做的是将一个 csv 文件读入一个 DataFrame 对象。
I want to preserve the functionality of the DataFrame object while adding in new attributes to the existing object. Thus I wrote a class called Basketball:
我想在向现有对象添加新属性的同时保留 DataFrame 对象的功能。于是我写了一个叫做 Basketball 的类:
from data_math import *
import pandas as pd
class Basketball(pd.DataFrame):
def __init__(self,*args,**kargs):
pd.DataFrame.__init__(self,*args,**kargs)
self.FGM = calculate_FGM(pd.DataFrame)
self.FGA = calculate_FGA(pd.DateFrame)
self.FGP = self.FGM / self.FGA
self.M3 = calculate_3M(pd.DataFrame)
self.A3 = calcualte_3A(pd.DataFrame)
self.P3 = self.M3 / self.A3
self.FTM = calcualte_FTM(pd.DataFrame)
self.FTA = calculate_FTA(pd.DataFrame)
self.FTP = self.FTM / self.FTA
# self.P = score_calculate(pd.DataFrame)
I wrote another data_math.pyfile to help calculate the different attributes I wanted to include into the Basketball class.
我编写了另一个data_math.py文件来帮助计算我想要包含在 Basketball 类中的不同属性。
from pandas import DataFrame
def score_calculate(df):
df_pt_scored = df[((df['etype']=='shot') & (df['result']=='made'))]
df_ft_scored = df[((df['etype']=='free throw') & (df['result']=='made'))]
return df_pt_scored['points'].sum()+len(df_ft_scored.index)
def calculate_FGM(df):
cond_pt = (df['etype']=='shots') & (df['results']=='made')
cond_ft = (df['etype']=='freethrow') & (df['results']=='made')
return len(df[cond_pt].index)+len(df[cond_ft].index)
def calculate_FGA(df):
shot_cond= df['etype']=='shot'
free_throw_cond = df['etype']=='free throw'
return len(df[shot_cond].index)+len(df[free_throw_cond].index)
def calculate_3M(df):
cond_3M= (df['etype']=='shot')&(df['type']=='3pt')&(df['result']=='made')
return len(df[cond_3M].index)
def calcualte_3A(df):
cond_3A = (df['etype']=='shot')&(df['type']=='3pt')
return len(df[cond_3A].index)
def calculate_FTM(df):
cond_FTM =(df['etype']=='free throw') & (df['result']=='made')
return len(df[cond_FTM].index)
def calcualte_FTA(df):
cond_FTA =(df['etype']=='free throw')
return len(df[cond_FTA].index)
In the end I start my program from main.py which I hope would give me the correct output. However while executing on this line:
最后,我从 main.py 开始我的程序,我希望它能给我正确的输出。但是,在此行上执行时:
team1= Basketball(tm1)
I received the following Traceback
我收到以下回溯
Traceback (most recent call last):
File "/Users/luoyicheng/Developer/STAR-Research/data_analysis/source code/main.py", line 20, in <module>
team1= Basketball(tm1)
File "/Users/luoyicheng/Developer/STAR-Research/data_analysis/source code/Basketball.py", line 6, in __init__
self.FGM = calculate_FGM(pd.DataFrame)
File "/Users/luoyicheng/Developer/STAR-Research/data_analysis/source code/data_math.py", line 9, in calculate_FGM
cond_pt = (df['etype']=='shots') & (df['results']=='made')
TypeError: 'type' object has no attribute '__getitem__'
I am new to python programming and could not figure out why this error has occurred. To my understanding, this error means I am unable to use indexing feature of the DataFrame. However, if I try to code in my main function similar things I am able to get the output I want. I am also not clear of how to extend the existing DataFrame class so that I can still access the methods in the DataFrame class while extending the team1 object to have attributes such as FGM, FGA, etc.
我是 python 编程的新手,无法弄清楚为什么会发生这个错误。据我了解,此错误意味着我无法使用 DataFrame 的索引功能。但是,如果我尝试在我的主函数中编写类似的代码,我可以获得我想要的输出。我也不清楚如何扩展现有的 DataFrame 类,以便在扩展 team1 对象以具有 FGM、FGA 等属性的同时仍然可以访问 DataFrame 类中的方法。
The idea of extending this class is to allow me to pass any DataFrame object in the Basketball() so that I can have an object with extending attributes and methods. I think I also lack an understanding of the use of initand self.
扩展这个类的想法是允许我在 Basketball() 中传递任何 DataFrame 对象,以便我可以拥有一个具有扩展属性和方法的对象。我想我也对init和self的使用缺乏了解。
Please don't blame for not describing the problem clearly as I am not familiar with all the terminology in OOP.
请不要怪我没有清楚地描述问题,因为我不熟悉 OOP 中的所有术语。
Thank you so much!
非常感谢!
采纳答案by Andy Hayden
You're passing each function pd.DataFramewhich is of type type:
您正在传递pd.DataFrame类型为 type 的每个函数:
In [11]: type(pd.DataFrame)
Out[11]: type
Hence the exception message.
因此出现异常消息。
You mean to be passing self (which is of type DataFrame):
您的意思是要传递 self (属于 DataFrame 类型):
self.FGM = calculate_FGM(pd.DataFrame)
...
should read:
应该读:
self.FGM = calculate_FGM(self)
...

