Python for循环错误:序列中的真值不明确

Python for循环错误:序列中的真值不明确,python,pandas,for-loop,series,Python,Pandas,For Loop,Series,错误表示序列的真值不明确。错误发生在第8行。 我不确定我在这个for循环中做错了什么。如果您想查看数据帧,我可以将其发布到GitHub上。提前感谢您的帮助 1) charged_off = 0 2) fully_paid = 0 3) percentages = [] 4) index = [] 5) 6) for var in emp_length: 7) for i in range(0,10): 8) if loan['emp_length'] == emp_le

错误表示序列的真值不明确。错误发生在第8行。 我不确定我在这个for循环中做错了什么。如果您想查看数据帧,我可以将其发布到GitHub上。提前感谢您的帮助

1) charged_off = 0
2) fully_paid = 0
3) percentages = []
4) index = []
5) 
6) for var in emp_length:
7)     for i in range(0,10):
8)         if loan['emp_length'] == emp_length[i] and loan['loan_status'] == 'Charged Off':
9)            charged_off = +1
10)        else: fully_paid = +1
11)        
12)    percentages.append((charged_off / (charged_off + fully_paid))*100)
13)
14) print(percentages)
emp_长度变量列表输出: [“1年”, “10年以上”, “2年”, “3年”, “4年”, “5年”, “6年”, “7年”, “8年”, “9年”, “<1年”]


loan['loan_status']。唯一输出:数组['Full Paid','Charged Off',dtype=object

如果我理解正确,则加载变量是一个数据帧,并且在执行此操作时 loan[emp_lengh]==emp_length[i]它将返回一系列布尔值,也就是一个面具


您必须至少写入您试图访问的索引行

我认为它将返回一系列布尔值,是的。如何写入索引行?要访问索引I,可以使用load.iloc[I]和loan.iloc[I][emp_length]访问emp_length值谢谢您的帮助!我会试试那种方法