【发布时间】:2020-12-22 06:19:31
【问题描述】:
我正在尝试使用循环下面的代码来计算关闭和打开的交付公司的 df 中的行数的百分比,但我收到 num_total = lead_df.Status[0] 行的错误,这应该可以给我 # 行就像前面几行的代码一样,但给我一个KeyError: 0的错误@
leads = ['Closed','Open']
max_status = None
max_percent = None
for lead in leads:
df_overall = df[(df['Status']== lead)]
num_overall = df_overall.Status[0]
lead_df = df[(df['Grubhub']== True)]
num_total = lead_df.Status[0]
percentage_overall = num_overall / num_total
if max_status is None:
print(lead, percentage_overall)
df 的样子:
Status | Grubhub
Closed True
Open True
Open False
我不知道我在 num_total 行中缺少什么,因为在单独的单元格中运行 df[(df['Grubhub']== True)] 时它应该可以工作,它会给我 400 行,谢谢!
【问题讨论】:
-
num_overall = df_overall.Status.iat[0]工作怎么样? -
如果我将 .iat[0] 添加到 num total 和 num total 中,它会显示 TypeError: unsupported operand type(s) for /: 'str' and 'str' @jezrael
标签: python python-3.x pandas data-science