Python 数据帧在第一次循环后变为非类型
在第一个for循环运行之后,contDF设置为非类型对象。因此,它遇到了一个问题,因为contDF[col]在第二个循环中不存在。在第一个循环结束时,contDF是正确的。当第二个循环开始时,它如何变成无Python 数据帧在第一次循环后变为非类型,python,pandas,Python,Pandas,在第一个for循环运行之后,contDF设置为非类型对象。因此,它遇到了一个问题,因为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 c
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])
这是输出
抑郁症、躁郁症、精神分裂症:
#样本年龄\年抗生素\史\
0 10317.000049761 52过去一年我没有服用抗生素。1 10317.000040165 32过去一年我没有服用抗生素。
2 10317.000030322 43.0过去一年我没有服用抗生素。
3 10317.00002857 56.0我在过去一年中没有服用抗生素。
4 10317.00003818939.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,
虽然您没有在代码转储中包含相关代码,但它碰巧出现在您的回溯中:
masterDF = Lable_Controls_and_Exper([col_name], col_data_control, col_data_exper)
无论此函数返回什么,都将该结果分配给masterDF
由于函数中的任何位置都没有return
语句,因此它返回None
,这意味着您将None
分配给masterDF
请注意,您正在将一个元素的列表传递给第一个参数。所以循环只发生一次。下一次通过循环要等到下次调用此函数时
下次调用函数时,当它深度复制masterDF
时,它正在复制None
由于不清楚此代码试图做什么,因此很难告诉您如何修复它,但几乎可以肯定这是以下两件事之一:
masterDF
,或masterDF
的有用内容如果您能提供一个可以剪切粘贴的数据帧示例,这将非常有用。
masterDF = Lable_Controls_and_Exper([col_name], col_data_control, col_data_exper)