Python 在数据帧中对数据进行分区 TypeError回溯(最近一次调用) 在里面 ---->2个真行,假行=分区(训练数据,问题(0,1)) 3行 分区中(df,问题) 20个真行,假行=[],[] 21对于df中的行: --->22如果问题匹配(世界其他地区): 23行。追加(行) 24其他: 在匹配中(自我,示例) 14#问题中的特征值 15 --->16 val=示例[self.column] 17如果是数字(val): 18返回值>=self.value TypeError:“int”对象不可下标
我试图从一个决策树改编这段代码,这样它就可以应用到pandas数据帧而不是列表,我得到了错误“TypeError:“int”对象不可下标”,我如何返回一个计数或数据帧中的行列表,这些行是真的还是假的,而没有得到错误,我知道我没有正确地遍历数据帧,非常感谢所有帮助 该错误可能在迭代器中Python 在数据帧中对数据进行分区 TypeError回溯(最近一次调用) 在里面 ---->2个真行,假行=分区(训练数据,问题(0,1)) 3行 分区中(df,问题) 20个真行,假行=[],[] 21对于df中的行: --->22如果问题匹配(世界其他地区): 23行。追加(行) 24其他: 在匹配中(自我,示例) 14#问题中的特征值 15 --->16 val=示例[self.column] 17如果是数字(val): 18返回值>=self.value TypeError:“int”对象不可下标,python,pandas,dataframe,Python,Pandas,Dataframe,我试图从一个决策树改编这段代码,这样它就可以应用到pandas数据帧而不是列表,我得到了错误“TypeError:“int”对象不可下标”,我如何返回一个计数或数据帧中的行列表,这些行是真的还是假的,而没有得到错误,我知道我没有正确地遍历数据帧,非常感谢所有帮助 该错误可能在迭代器中不在行上迭代。它相当于df.columns中的column\u name的。您可能需要以下内容来获取所有行 TypeError Traceback (m
。您可能需要以下内容来获取所有行
TypeError Traceback (most recent call last)
<ipython-input-53-386e8df97e85> in <module>
----> 2 true_rows, false_rows = partition(training_data, Question(0,1))
3 true_rows
<ipython-input-52-9ff7f19eff20> in partition(df, question)
20 true_rows, false_rows = [],[]
21 for row in df:
---> 22 if question.match(row):
23 true_rows.append(row)
24 else:
<ipython-input-12-928374ee6f4e> in match(self, example)
14 #feature value in the question
15
---> 16 val = example[self.column]
17 if is_numeric(val):
18 return val >= self.value
TypeError: 'int' object is not subscriptable
也就是说,如果您为此创建一个列,您将获得更好的性能
for loc, row in df.iterrows():
...
然后,您可以执行类似于df.query('question\u A\u is\u true')
或df.loc[df['question\u A\u is\u true]]
的操作,以获得真正的行。如果你有多个问题,你可以很快地把它们组合起来,因为它们在引擎盖下使用numpy
def partition(df, question_column, true_value):
view_of_values = df[question_column]
example = view_of_values[0]
if is_numeric(example):
is_true = view_of_values >= true_value
else:
is_true = view_of_values == true_value
# is_true is a column of True, False values for each
# you could store this df['question_A_is_true'] = is_true
# for partitioning later.
return is_true
df['question_A_is_true'] = partition(df, 'question_A', 'the_truth_is_out_there')
您正在使用一个迭代器,其中有df方法可以根据您的需要进行条件选择。为了更简单,请包含错误的完整跟踪并提供一个示例,但问题似乎在于示例[self.column]
。当您对df中的行执行操作时
不会得到行,我相信您会得到索引。因此,example
是一个int,不能在int上使用索引符号(括号)。同样,我们只是在计算,因为我们没有一个有效的示例
def partition(df, question_column, true_value):
view_of_values = df[question_column]
example = view_of_values[0]
if is_numeric(example):
is_true = view_of_values >= true_value
else:
is_true = view_of_values == true_value
# is_true is a column of True, False values for each
# you could store this df['question_A_is_true'] = is_true
# for partitioning later.
return is_true
df['question_A_is_true'] = partition(df, 'question_A', 'the_truth_is_out_there')
df.loc[
(df['question_A_is_true'])
or (df['question_B_is_true'] and not df['question_C_is_true'])
]