pandas 类型错误:'DataFrame' 对象不可调用 python 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45845478/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:18:16  来源:igfitidea点击:

TypeError: 'DataFrame' object is not callable python function

pythonfunctionpandastypeerror

提问by KriKors

I have two functions, one which creates a dataframe from a csv and another which manipulates that dataframe. There is no problem the first time I pass the raw data through the lsc_age(import_data())functions. However, I get the above-referenced error (TypeError: 'DataFrame' object is not callable)upon second+ attempts. Any ideas for how to solve the problem?

我有两个函数,一个从 csv 创建数据帧,另一个操作该数据帧。我第一次通过lsc_age(import_data())函数传递原始数据没有问题。但是,我(TypeError: 'DataFrame' object is not callable)在第二次+尝试时收到上述错误。有关如何解决问题的任何想法?

def import_data(csv,date1,date2):
    global data
    data = pd.read_csv(csv,header=1)
    data = data.iloc[:,[0,1,4,6,7,8,9,11]]
    data = data.dropna(how='all')
    data = data.rename(columns={"National: For Dates 9//1//"+date1+" - 8//31//"+date2:'event','Unnamed: 1':'time','Unnamed: 4':'points',\
              'Unnamed: 6':'name','Unnamed: 7':'age','Unnamed: 8':'lsc','Unnamed: 9':'club','Unnamed: 11':'date'})
    data = data.reset_index().drop('index',axis=1)
    data = data[data.time!='Time']
    data = data[data.points!='Power ']
    data = data[data['event']!="National: For Dates 9//1//"+date1+" - 8//31//"+date2]
    data = data[data['event']!='USA Swimming, Inc.']
    data = data.reset_index().drop('index',axis=1)
    for i in range(len(data)):
        if len(str(data['event'][i])) <= 3:
            data['event'][i] = data['event'][i-1]
        else:
            data['event'][i] = data['event'][i]
    data = data.dropna()
    age = []
    event = []
    gender = []
    for row in data.event:
        gender.append(row.split(' ')[0])
        if row[:9]=='Female 10':
            n = 4
            groups = row.split(' ')
            age.append(' '.join(groups[1:n]))
            event.append(' '.join(groups[n:]))
        elif row[:7]=='Male 10':
            n = 4
            groups = row.split(' ')
            age.append(' '.join(groups[1:n]))
            event.append(' '.join(groups[n:]))
        else:
            n = 2
            groups = row.split(' ')
            event.append(' '.join(groups[n:]))
            groups = row.split(' ')
            age.append(groups[1])
    data['age_group'] = age
    data['event_simp'] = event
    data['gender'] = gender
    data['year'] = date2
    return data

def lsc_age(data_two):
    global lsc, lsc_age, top, all_performers
    lsc = pd.DataFrame(data_two['event'].groupby(data_two['lsc']).count()).reset_index().sort_values(by='event',ascending=False)
    lsc_age = data_two.groupby(['year','age_group','lsc'])['event'].count().reset_index().sort_values(by=['age_group','event'],ascending=False)
    top = pd.concat([lsc_age[lsc_age.age_group=='10 & under'].head(),lsc_age[lsc_age.age_group=='11-12'].head(),\
                 lsc_age[lsc_age.age_group=='13-14'].head(),lsc_age[lsc_age.age_group=='15-16'].head(),\
                 lsc_age[lsc_age.age_group=='17-18'].head()],ignore_index=True)
    all_performers = pd.concat([lsc_age[lsc_age.age_group=='10 & under'],lsc_age[lsc_age.age_group=='11-12'],\
                            lsc_age[lsc_age.age_group=='13-14'],lsc_age[lsc_age.age_group=='15-16'],\
                            lsc_age[lsc_age.age_group=='17-18']],ignore_index=True)
    all_performers = all_performers.rename(columns={'event':'no. top 100'})
    all_performers['age_year_lsc'] = all_performers.age_group+' '+all_performers.year.astype(str)+' '+all_performers.lsc
    return all_performers

years = [i for i in range(2008,2018)]
for i in range(len(years)-1):
    lsc_age(import_data(str(years[i+1])+"national100.csv",\
    str(years[i]),str(years[i+1])))

回答by silvanoe

During the first call to your function lsc_age()in line

在第一次调用你的函数lsc_age()的线

lsc_age = data_two.groupby(['year','age_group','lsc'])['event'].count().reset_index().sort_values(by=['age_group','event'],ascending=False)

you are overwriting your function object with a dataframe. This is happening since you imported the function object from the global namespace with

您正在用数据框覆盖您的函数对象。这是因为您从全局命名空间导入了函数对象

global lsc, lsc_age, top, all_performers

Functions in Python are objects. Please see more information about this here.

Python 中的函数是对象。请在此处查看更多信息。

To solve your problem, try to avoid the global imports. They do not seem to be necessary. Try to pass your data around through the arguments of the function.

要解决您的问题,请尽量避免全局导入。它们似乎没有必要。尝试通过函数的参数传递数据。