遍历两个列表的循环不起作用 [英] Loop for iterating through two lists is not working

查看:125
本文介绍了遍历两个列表的循环不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to use a loop for iterating through two lists. Unfortunately, the second for loop does not work: it only checks the first item within the list, but not with rest. Could you please tell me why?

谢谢

列表:

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

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

I would like to add two more columns (Cars and Planes) to my original dataset based on if statements:

  • 如果列表'Engine to check'中的对象在low_cars_engines列表中,则它是汽车,否则,它不是;
  • 如果列表检查的引擎"中的对象位于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循环工作正常,但第二个则不能:即使在low_planes_engines列表中,我也无法为检查的引擎"列表中的项分配值(它始终使我否").

The first for loop works fine, but not the second: I am not able to assign a value to an item in the list 'Engine to check' even if it is within the list low_planes_engines (it gives me always No).

能否请您告诉我什么地方出了问题,以及是否可以仅使用一个for循环而不是两个?我希望保留相同的结构,或保留df.at[index,'_'].现在,第二个循环仅打印/检查list_lowsplanes_engines列表中的第一项(即Pratt& Whitney),而其余部分则不进行检查.

Could you please tell me what is wrong and if it would be possible to use only one for loop rather than two? I would prefer to keep the same structure, or keep df.at[index,'_']. Right now the second loop print/check only the first item of the list low_planes_engines (i.e. Pratt & Whitney) and does not go through the rest.

由于数据集类似于:

Audi
CFM International
Rolls-Royce
Bentley
Volkswagen
Toyota
Suzuki
Porsche

并且不包含该元素,Planes下的所有行均设置为No.

and it does not include that element, all the rows under Planes are set to No.

推荐答案

在使用Pandas时,不应使用循环. DataFrame不能按顺序访问.不过,您需要一些NumPy:

You should not use loops when you work with Pandas. DataFrames are not designed to be accessed sequentially. You need some NumPy, though:

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,因为它们将来会更易于使用:

You probably should not use "Yes" and "No," either. Use boolean values True and False instead, as they are easier to work with in the future:

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

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

Finally, if everything in the DataFrame is strictly a car or a plane, only one column is required. The other will be the complement.

这篇关于遍历两个列表的循环不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆