TypeError:'DataFrame'对象不是可调用的python函数

时间:2017-08-23 16:58:22

标签: python function pandas typeerror

我有两个函数,一个用于从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])))

1 个答案:

答案 0 :(得分:6)

在第一次调用函数lsc_age()的行中

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

您正在使用数据框覆盖您的函数对象。这是因为您使用

从全局命名空间导入了函数对象
global lsc, lsc_age, top, all_performers

Python中的函数是对象。请参阅有关此here的更多信息。

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