在第一个for循环运行后,将contDF设置为Nonetype对象。由于contDF [col]在第二个循环中不存在,因此出现了问题。在第一个循环结束时,contDF是正确的。有些东西在第二个循环开始时变成无。
def Lable_Controls_and_Exper(colum_name, list_values_to_determine_control, list_values_to_determine_exper):
contDF = copy.deepcopy(masterDF)
for col in colum_name:
index = 0
print(col+':')
print(contDF)
print(contDF[col])
for element in contDF[col]:
#print(index)
#if list values match a row in a column then the row is excluded
for value in list_values_to_determine_control:
if(element == value
and contDF.loc[index,'control_or_experiment'] != 'exper'):
contDF.loc[index,'control_or_experiment'] = 'control'
for value in list_values_to_determine_exper:
if(element == value):
contDF.loc[ index, 'control_or_experiment' ] = 'exper'
index = index + 1
print(contDF)
print(contDF[col])
这是输出。
抑郁症双相精神分裂症: #SampleID age_years抗生素_history \ 0 10317.000049761 52我在去年没有服用抗生素。
1 10317.000040165 32过去一年我没有服用抗生素。
2 10317.000030322 43.0我在去年没有服用抗生素。
3 10317.000028857 56.0过去一年我没有服用抗生素。
4 10317.000038189 39.0过去一年我没有服用抗生素。
5 10317.000001281 61.0在过去的一年中我没有服用抗生素。
6 10317.000036487 46.0过去一年中我没有服用抗生素。bmi_cat depression_bipolar_schizophrenia \ 0 Overweight Unspecified 1 Unspecified Unspecified 2 Normal I do not have this condition 3 Overweight I do not have this condition 4 Overweight Unspecified 5 Unspecified Unspecified 6 Obese I do not have this condition mental_illness_type_depression smoking_frequency control_or_experiment 0 Unspecified Never none 1 Unspecified Never none 2 Unspecified Never none 3 Unspecified Never none 4 Unspecified Never none 5 Unspecified Never none 6 Unspecified Never none 0 Unspecified 1 Unspecified 2 I do not have this condition 3 I do not have this condition 4 Unspecified 5 Unspecified 6 I do not have this condition Name: depression_bipolar_schizophrenia, dtype: object #SampleID age_years antibiotic_history \ 0 10317.000049761 52 I have not taken antibiotics in the past year. 1 10317.000040165 32 I have not taken antibiotics in the past year. 2 10317.000030322 43.0 I have not taken antibiotics in the past year. 3 10317.000028857 56.0 I have not taken antibiotics in the past year. 4 10317.000038189 39.0 I have not taken antibiotics in the past year. 5 10317.000001281 61.0 I have not taken antibiotics in the past year. 6 10317.000036487 46.0 I have not taken antibiotics in the past year. bmi_cat depression_bipolar_schizophrenia \ 0 Overweight Unspecified 1 Unspecified Unspecified 2 Normal I do not have this condition 3 Overweight I do not have this condition 4 Overweight Unspecified 5 Unspecified Unspecified 6 Obese I do not have this condition mental_illness_type_depression smoking_frequency control_or_experiment 0 Unspecified Never control 1 Unspecified Never control 2 Unspecified Never control 3 Unspecified Never control 4 Unspecified Never control 5 Unspecified Never control 6 Unspecified Never control 0 Unspecified 1 Unspecified 2 I do not have this condition 3 I do not have this condition 4 Unspecified 5 Unspecified 6 I do not have this condition Name: depression_bipolar_schizophrenia, dtype: object mental_illness_type_depression: None --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-13-365067e6465a> in <module>() 11 col_data_control = user_input_controlDF[col_name].tolist() 12 col_data_exper = user_input_experimentDF[col_name].tolist() ---> 13 masterDF = Lable_Controls_and_Exper([col_name], col_data_control, col_data_exper) 14 while_count_two = while_count_two + 1 15 print("done") <ipython-input-12-3898e2174ad7> in Lable_Controls_and_Exper(colum_name, list_values_to_determine_control,
list_values_to_determine_exper) 6个打印(col +':') 7张(contDF) ----> 8个print(contDF [col]) 9 for contDF [col]中的元素: 10 #print(index)
TypeError: 'NoneType' object is not subscriptable
答案 0 :(得分:0)
虽然您没有在代码转储中包含相关代码,但是它恰好出现在您的回溯中:
masterDF = Lable_Controls_and_Exper([col_name], col_data_control, col_data_exper)
无论此函数返回什么,您都将结果分配给masterDF
。
由于该函数中的任何地方都没有return
语句,因此它返回None
,这意味着您将None
分配给masterDF
。
请注意,您正在将[col_name]
(一个元素的列表)传递给第一个参数。因此,循环仅发生一次。直到您下次调用此函数时,才会循环下一次。
下次调用该函数时,当它深度复制masterDF
时,它就是在复制None
。
由于尚不清楚该代码要做什么,因此很难告诉您如何解决它,但这几乎可以肯定是以下两件事之一:
masterDF
,或masterDF
的有用内容。