类型错误:“method”类型的对象没有len()

2024-09-26 22:53:15 发布

您现在位置:Python中文网/ 问答频道 /正文

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))

嘿伙计们。我试图写一个脚本,将计算医疗任务的端点。当我运行程序时,它会一直工作,直到到达脚本的第一个for循环。我很确定有一个错误,因为CategoryID没有length属性。但我也认为应该这样做,因为我在一开始就把它转换成一个列表。关于如何解决这个问题有什么建议吗?提前谢谢。


Tags: ofcsvtoyoudataframeinputoutputwhat
1条回答
网友
1楼 · 发布于 2024-09-26 22:53:15

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

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

相关问题 更多 >

    热门问题