pandas 类型错误:“方法”类型的对象没有 len()

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

TypeError: object of type 'method' has no len()

pythonpython-3.xpandas

提问by Ryan Chen

def DS():
import os
import pandas as pd

directory=input('What folder would you like to work in? ( Example: /Users/hem/Desktop/pythontest/ ')
filename=input('Please enter the name (including .csv) of the file you would like to analyze  ' ) 
idkey=input('What is the subject ID you are analyzing?    '   )
sessionkey=input('What session of testing are you analyzing?   ')      
print('---------- Calculating Drug Stroop endpoints ----------')
os.chdir(directory)
dataframe = pd.read_csv(filename, error_bad_lines=False)
output={}

CategoryID = dataframe['CategoryID'].tolist
ReactionTime = dataframe['ReactionTime'].tolist
CorrectResponse = dataframe['CorrectResponse'].tolist

#Stroop interference score
totalN = 0
countN = 0
CorrectResponseNeutral = 0
for i in range(len(CategoryID)):
    if CategoryID[i] == 1:
        totalN + ReactionTime[i]
        countN + 1
        CorrectResponseNeutral + CorrectResponse[i]

AverageRTNeutral = totalN/countN  
CorrectResponseNeutral = CorrectResponseNeutral/countN

totalD = 0 
countD = 0
CorrectResponseDrug = 0
for i in range(len(CategoryID)):
    if CategoryID[i] == 2:
        totalD + ReactionTime[i]
        countD + 1
        CorrectResponseDrug + CorrectResponse[i]

AverageRTDrug = totalD/countD
CorrectResponseDrug = CorrectResponseDrug/countD
InterferenceScore =  AverageRTNeutral - AverageRTDrug       


output['SubjectID'] = idkey 
output['Session'] = sessionkey
output['Interference Score'] = InterferenceScore
output['Accuracy of Neutral Trials'] = CorrectResponseNeutral
output['Accuracy of Drug Trials'] = CorrectResponseDrug
print('---------- Done calculating endpoints ----------')
outputname=input('What would you like to name your outputfile? (Please include.csv)')

outputdataframe = pd.DataFrame.from_dict([output])
outputdataframe.to_csv(os.path.join('/Users/hem/Desktop/Analysis/DrugStroopAnalyzed',outputname))

Hey Guys. Im trying to write a script that would calculate endpoints for a medical task. When I run the program, it works all the way until it hits the first for loop of the script. I'm pretty sure there is an error because CategoryID doesnt have a length property. But I also think it should because I'm converting it to a list in the beginning. Any suggestions on how to fix this? Thanks in advance.

大家好。我正在尝试编写一个脚本来计算医疗任务的端点。当我运行程序时,它会一直运行,直到遇到脚本的第一个 for 循环。我很确定存在错误,因为 CategoryID 没有长度属性。但我也认为应该这样做,因为我一开始就将其转换为列表。对于如何解决这个问题,有任何的建议吗?提前致谢。

回答by Christian

It seems like you forgot the ()after tolistmethod, so it can be parsed as a call to the method, and not the method itself:

似乎您忘记了()aftertolist方法,因此可以将其解析为对方法的调用,而不是方法本身:

CategoryID = dataframe['CategoryID'].tolist()
ReactionTime = dataframe['ReactionTime'].tolist()
CorrectResponse = dataframe['CorrectResponse'].tolist()