python如果声明有多个批评

时间:2015-10-08 16:12:54

标签: python numpy pandas

我的目标很简单。

从下面的数据框(df3):

如果mr_no等于定义的数字(我有下面的列表)然后开火,如果fire1等于覆盖然后开火,如果fire2等于主要或次要然后开火,否则如果这三个都没有符合标准而不是火。

DF3:

     mr_no        fire1    fire2
0   A5155       covered  primary
1   FS2328      covered  neither
3   133158  not_covered  primary
4   FS2328  not_covered  neither
5   A5128   not_covered  neither
6   A5128   not_covered  neither
7   133158      covered  primary
8   133158  not_covered  neither
9   A5128   not_covered  neither
10  A5129   not_covered  neither

我尝试了以下内容来提取我需要的代码:

    mr = ["A5115","133158","FS2114","FS2110","FS2115","FS2111","FS2149","FS2150","FS2017","FS2018"]

def fire(row):
    if row['fire1'] == ('covered') + row['fire2'] == ('primary') | ('secondary') + row['mr_no'] == (mr):
        return 'fire'
    return 'not fire'

df3['fire_final'] = df3.apply (lambda row: fire(row),axis=1)

但是,我的结果没有找到,但我知道应该有一些东西:

IN:  df3.groupby('fire_final').ssno.count()

OUT:  fire_final
not fire      214166
Name: ssno, dtype: int64

我认为我的错误与我的列表有关,但我的编程知识仍然是新手所以我不确定如何为列表做条件。

与往常一样,我非常感谢您的帮助。

更新:

尝试edChum推荐的代码会产生结果,但正如你所看到的那样,行120044和120045显示fire1被“覆盖”而fire2是主要的,但是它们不是火,但它们应该显示为火。

df3['fire_final'] = np.where((df3['mr_no'].isin(mr)) & (df3['fire1'] == 'covered') & (df3['fire2'].isin(['primary','secondary'])), 'fire', 'not fire')


         mr_no        fire1      fire2   fire_final
120020  203670  not_covered    neither   not fire
120021  001500  not_covered    neither   not fire
120022  5273    not_covered    neither   not fire
120023  190365  not_covered    neither   not fire
120024  A5155   not_covered    neither   not fire
120025  236707  not_covered    neither   not fire
120026  A9088   not_covered    primary   not fire
120027  A9088   not_covered    primary   not fire
120028  FS1476  not_covered    primary   not fire
120029  A9092   not_covered    primary   not fire
120031  FS1476  not_covered    neither   not fire
120032  FS1476  not_covered    neither   not fire
120033  AFM002  not_covered    primary   not fire
120034  A9092   not_covered    primary   not fire
120035  FS1476  not_covered    primary   not fire
120036  FS1476  not_covered    primary   not fire
120037  007881  not_covered    neither   not fire
120038  007286  not_covered    neither   not fire
120039  A010    not_covered    neither   not fire
120041  N5012   not_covered    neither   not fire
120042  FS1478      covered    primary       fire
120043  FS1478      covered    primary       fire
120044  A2075       covered    primary   not fire
120045  136492      covered    primary   not fire

更新:

当我使用|时,EdChum的推荐工作而不是&

df3['fire_final'] = np.where((df3['mr_no'].isin(mr)) | (df3['fire1'] == 'covered') | (df3['fire2'].isin(['primary','secondary'])), 'fire', 'not fire')

产生预期的结果:

            mr_no        fire1      fire2 fire_final
120062  FS0937  not_covered  secondary       fire
120063  FS0937  not_covered  secondary       fire
120064  FS0900  not_covered  secondary       fire
120066  FS2037      covered    primary       fire
120067  A9090   not_covered    neither   not fire
120068  129440      covered    neither       fire
120069  FS1629      covered    primary       fire
120070  FS2037      covered    primary       fire
120071  FS2037      covered    primary       fire
120072  A9074   not_covered    neither   not fire
120073  ABE102  not_covered    neither   not fire
120074  ABE100  not_covered    neither   not fire
120075  A5125   not_covered    neither   not fire
120076  199424  not_covered    neither   not fire
120077  179873  not_covered    neither   not fire
120078  A021    not_covered    neither   not fire
120079  A022    not_covered    neither   not fire
120080  A022    not_covered    neither   not fire
120081  A022    not_covered    neither   not fire

2 个答案:

答案 0 :(得分:2)

Python不是C ++也不是C.因此,它使用了一些不同的结构。

在您的情况下,当您希望在if为真时 thing1为真时触发thing2语句,您使用or不是|

在Python中|执行按位or操作。

所以你想要这样的东西:

if (something) or (something):
    return 'fired'

答案 1 :(得分:1)

我相信你可以使用np.where

在单行中执行此操作
In [6]:
mr = ["A5115","133158","FS2114","FS2110","FS2115","FS2111","FS2149","FS2150","FS2017","FS2018"]
df['fire_final'] = np.where((df['mr_no'].isin(mr)) & (df['fire1'] == 'covered') & (df['fire2'].isin(['primary','secondary'])), 'fire', 'not fire')
df

Out[6]:
     mr_no        fire1    fire2 fire_final
0    A5155      covered  primary   not fire
1   FS2328      covered  neither   not fire
3   133158  not_covered  primary   not fire
4   FS2328  not_covered  neither   not fire
5    A5128  not_covered  neither   not fire
6    A5128  not_covered  neither   not fire
7   133158      covered  primary       fire
8   133158  not_covered  neither   not fire
9    A5128  not_covered  neither   not fire
10   A5129  not_covered  neither   not fire

目前还不清楚你真正想要的是什么,但如果任何条件为真,你想要返回true,那么你需要或条件:

df['fire_final'] = np.where((df['mr_no'].isin(mr)) | (df['fire1'] == 'covered') | (df['fire2'].isin(['primary','secondary'])), 'fire', 'not fire')

需要注意的事项是,对于数组,您需要对&|和{{1}使用按位运算符~andor这是因为我们在这里比较一个数组与一些布尔条件,我们想要一个返回的布尔数组。对于多个条件,由于运算符优先级,您需要将条件括在not中。