python - 类型错误 : 'type' object has no attribute '__getitem__' in pandas DataFrame

标签 python pandas

我试图分析一支篮球队的逐场比赛数据 我所做的是将 csv 文件读入 DataFrame 对象。

我想保留 DataFrame 对象的功能,同时向现有对象添加新属性。因此我写了一个名为篮球的类:

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)

我编写了另一个 data_math.py 文件来帮助计算我想要包含到篮球类中的不同属性。

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)

最后,我从 main.py 启动我的程序,我希望它能给我正确的输出。然而,在这一行执行时:

team1= Basketball(tm1)

我收到了以下回溯

 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__'

我是Python编程新手,无法弄清楚为什么会发生这个错误。据我了解,此错误意味着我无法使用 DataFrame 的索引功能。但是,如果我尝试在主函数中编写类似的代码,我就能得到我想要的输出。我也不清楚如何扩展现有的 DataFrame 类,以便我仍然可以访问 DataFrame 类中的方法,同时扩展 team1 对象以具有 FGM、FGA 等属性。

扩展这个类的想法是允许我在Basketball()中传递任何DataFrame对象,这样我就可以拥有一个具有扩展属性和方法的对象。我想我对 init 和 self 的使用也缺乏理解。

请不要责怪我没有清楚地描述问题,因为我不熟悉OOP中的所有术语。

非常感谢!

最佳答案

您正在传递每个类型为 type 的函数 pd.DataFrame:

In [11]: type(pd.DataFrame)
Out[11]: type

因此出现异常消息。

您的意思是传递 self (属于 DataFrame 类型):

self.FGM = calculate_FGM(pd.DataFrame)
...

应阅读:

self.FGM = calculate_FGM(self)
...

关于python - 类型错误 : 'type' object has no attribute '__getitem__' in pandas DataFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24320862/

相关文章:

python - Django 的 IPython Notebook session

python - 如何获得连续第二高的值?

python - 从 Python 运行 Perl 代码(输出到文件)

python - 检查 numpy 数组中的不变列

Python:如何合并具有相同名称 "Ocode"或“数据帧中的代码”的行

python - 使用外部 url 时的 Pandas read_csv 响应代码

csv - 使用 MultiIndex 读取 CSV 文件

python - 按行值比较数据帧列并选择具有匹配模式的列

python - 在 Pandas 中创建类似相关矩阵的数据框

python - 如何在 Nginx 中故意引发 400 Bad Request?