【问题标题】:Loop for iterating through two lists is not working循环遍历两个列表不起作用
【发布时间】:2020-07-19 01:41:01
【问题描述】:

我正在尝试使用循环来遍历两个列表。不幸的是,第二个 for 循环不起作用:它只检查列表中的第一项,而不是其余的。 你能告诉我为什么吗?

谢谢

列表:

low_cars_engines=['Audi', 'Bentley', 'Bugatti', 'Porsche', 'Skoda']
low_planes_engines=['Pratt & Whitney','Rolls-Royce','GE Aviation']

我想根据 if 语句在我的原始数据集中再添加两列(汽车和飞机):

  • 如果列表 'Engine to check' 中的对象在列表 low_cars_engines 中,则它是汽车,否则,它不是;
  • 如果列表“Engine to check”中的对象在列表low_planes_engines 中,则它是平面,否则不是。
import re

df['Cars'] = pd.Series(index = df.index, dtype='object')
df['Planes'] = pd.Series(index = df.index, dtype='object')

for index, row in df.iterrows():
    value = row['Engine to check']
    for x in low_cars_engines:
        if x in value:
            print(x)
            df.at[index,'Cars'] = 'Yes' # need to keep df.at[index, '_']
            break
        else: 
            df.at[index,'Cars'] = 'No' # need to keep df.at[index, '_']
            break

for index, row in df.iterrows():
    value = row['Engine to check']
    for x in low_planes_engines:
        if x in value:
            df.at[index,'Planes'] = 'Yes'
            break
        else: 
            df[index,'Planes'] = 'No'
            break

print(df)

第一个 for 循环工作正常,但不是第二个:我无法为列表“Engine to check”中的项目分配值,即使它在列表 low_planes_engines 中(它总是给我否)。

您能告诉我哪里出了问题吗?是否可以只使用一个 for 循环而不是两个?我宁愿保持相同的结构,或者保持df.at[index,'_']。现在,第二个循环仅打印/检查列表 low_planes_engines 的第一项(即 Pratt & Whitney),其余的不进行。

由于数据集类似于:

Audi
CFM International
Rolls-Royce
Bentley
Volkswagen
Toyota
Suzuki
Porsche

并且它不包括该元素,Planes 下的所有行都设置为No

【问题讨论】:

  • 你在两个if 分支中都有break,所以循环永远不会继续。
  • “要检查的引擎”列中有什么内容?第二次出现的空格可能是无意的,并阻止了第二次循环的工作。
  • 是的,抱歉,这是一个错字。我固定在帖子里。代码中没有空格。复制到这里是我的错
  • 你可以用df["Cars"] = df['Engine to check'].isin(low_cars_engines)替换你的循环
  • 我试过了,但它把所有的值都设置为 True

标签: python pandas loops for-loop if-statement


【解决方案1】:

你这里有一个额外的空间

row['Engine to check ']

试试改成

row['Engine to check']

【讨论】:

  • 不是因为这个。抱歉,这里是复制代码时的拼写错误。我应该如何编辑语句中的中断?
  • 我会使用 lambda 表达式并应用易于阅读且不涉及循环的函数,例如 df['Cars'] = df.apply(lambda x: 'Yes' if x['Engine to check'] in low_cars_engines else 'No', axis=1)df['Planes'] = df.apply(lambda x: 'Yes' if x['Engine to check'] in low_planes_engines else 'No', axis=1)
【解决方案2】:

在使用 Pandas 时不应使用循环。 DataFrames 不是为顺序访问而设计的。不过,你需要一些 NumPy:

import numpy as np
df['Cars']   = np.where(df['Engine to check'].isin(low_cars_engines), 'Yes', 'No') 
df['Planes'] = np.where(df['Engine to check'].isin(low_planes_engines), 'Yes', 'No')

结果:

#     Engine to check Cars Planes
# 0               Audi  Yes     No
# 1  CFM International   No     No
# 2        Rolls-Royce   No    Yes
# 3            Bentley  Yes     No
# 4         Volkswagen   No     No
# 5             Toyota   No     No
# 6             Suzuki   No     No
# 7            Porsche  Yes     No

您可能也不应该使用“是”和“否”。请改用布尔值 TrueFalse,因为它们将来更易于使用:

df['Cars']   = df['Engine to check'].isin(low_cars_engines) 
df['Planes'] = df['Engine to check'].isin(low_planes_engines)

最后,如果 DataFrame 中的所有内容严格来说都是汽车或飞机,则只需要一列。另一个是补语。

【讨论】:

  • 问题是当这不是真时将所有值设置为真/假
  • 不,它没有。我包括了用你的数据计算的结果。
  • 我不知道我的代码中发生了什么,但它不起作用。你也可以试试这个列表吗? (我将在几分钟内更新数据集)。谢谢
  • 我更新了代码。你能试试这个新数据集吗?我认为有些东西不起作用(可能在程序中,而不是在代码中)。它应该做的差不多。非常感谢
  • 等等,您将网站地址与引擎名称进行比较,您希望它们匹配吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-25
  • 1970-01-01
  • 1970-01-01
  • 2012-03-23
  • 2016-02-04
  • 1970-01-01
相关资源
最近更新 更多