【问题标题】:Python for loop error: truth value in series is ambiguous [duplicate]Python for循环错误:系列中的真值不明确[重复]
【发布时间】:2021-06-26 16:45:39
【问题描述】:

错误表明该系列的真值不明确。错误发生在第 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_length 变量(列表输出): ['1年', '10 年以上', '2年', '3年', '4年', '5年', '6年', '7年', '8年', '9年', '

error message screenshot is here

loan['loan_status'].unique() 输出:array(['Fully Paid', 'Charged Off'], dtype=object)

【问题讨论】:

    标签: python pandas for-loop series


    【解决方案1】:

    如果我理解正确,负载变量是 pandas DataFrame,当你这样做时 loan["emp_lenght"]==emp_length[i] 它将返回一系列布尔值,也就是掩码。

    你必须至少写下你试图访问的索引(行)

    【讨论】:

    • 我认为它会返回一系列布尔值,是的。我将如何编写索引行?
    • 要访问索引i,可以使用load.iloc[i]loan.iloc[i]["emp_length"]访问emp_length值
    • 感谢您的帮助!我会试试那个方法
    猜你喜欢
    • 2020-08-11
    • 2018-01-11
    • 1970-01-01
    • 2019-08-13
    • 2019-07-08
    • 2017-12-28
    • 2020-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多