Python 熊猫保留符合或不符合条件的两行

Python 熊猫保留符合或不符合条件的两行,python,pandas,dataframe,Python,Pandas,Dataframe,对于一个像这样的df d = {'age' : [21, 45, 45, 5], 'salary' : [20, 40, 10, 100]} df = pd.DataFrame(d) df age salary 0 21 20 1 45 40 2 45 10 3 5 100 如果满足条件,我将尝试添加一个带有布尔值的列 df['stat'] = df['salary'] < 40 df age salary

对于一个像这样的df

d = {'age' : [21, 45, 45, 5],
'salary' : [20, 40, 10, 100]}

df = pd.DataFrame(d)

df
   age  salary
0   21      20
1   45      40
2   45      10
3    5     100
如果满足条件,我将尝试添加一个带有布尔值的列

df['stat'] = df['salary'] < 40
df
   age  salary   stat
0   21      20   True
1   45      40  False
2   45      10   True
3    5     100  False
df['stat']=df['salary']<40
df
年龄工资统计
0 21 20正确
14540错误
24510对
35100假
然而,当我将相同的条件赋给变量时,我看不到true或false列

x1 = df['salary'] < 40
df[x1]
   age  salary
0   21      20
2   45      10
x1=df['salary']<40
df[x1]
年薪
0   21      20
2   45      10
如果满足条件,除了添加列外,保留所有行的最佳方法是什么

如果我有多种情况,比如

>>> x2 = df['age'] < 25
df[x1 & x2]
   age  salary
0   21      20
>>x2=df['age']<25
df[x1和x2]
年薪
0   21      20
我想返回所有的行,但带有一个stat列,该列将指示T或F

然而,当我将相同的条件赋给变量时,我看不到true或false列

x1 = df['salary'] < 40
df[x1]
   age  salary
0   21      20
2   45      10
这是因为使用
df[x1]
,pandas执行操作,您只得到满足条件的行

df['stat'] = np.where(df['age'] < 25,np.where(df['salary'] < 40,True,False),False)
保留所有行,但在满足条件时添加列

使用第一种方法,您可以将多个条件链接起来,并将其全部分配给一个新列,而不是使用它们进行索引:

>>> df["stat"] = x1 & x2
>>> df

   age  salary   stat
0   21      20   True
1   45      40  False
2   45      10  False
3    5     100  False
然而,当我将相同的条件赋给变量时,我看不到true或false列

x1 = df['salary'] < 40
df[x1]
   age  salary
0   21      20
2   45      10
这是因为使用
df[x1]
,pandas执行操作,您只得到满足条件的行

df['stat'] = np.where(df['age'] < 25,np.where(df['salary'] < 40,True,False),False)
保留所有行,但在满足条件时添加列

使用第一种方法,您可以将多个条件链接起来,并将其全部分配给一个新列,而不是使用它们进行索引:

>>> df["stat"] = x1 & x2
>>> df

   age  salary   stat
0   21      20   True
1   45      40  False
2   45      10  False
3    5     100  False

您可以在此处使用应用功能:

df['stat'] = df.apply(lambda x: (x["age"] < 25) & (x["salary"] < 40),axis = 1)
df['stat']=df.apply(λx:(x[“年龄”]<25)和(x[“工资”]<40),axis=1)
或者为了获得更好的性能,请使用np.where 在np中,您可以使用任意数量的条件

df['stat'] = np.where(df['age'] < 25,np.where(df['salary'] < 40,True,False),False)
df['stat']=np.where(df['age']<25,np.where(df['salary']<40,True,False),False)

您可以在此处使用应用功能:

df['stat'] = df.apply(lambda x: (x["age"] < 25) & (x["salary"] < 40),axis = 1)
df['stat']=df.apply(λx:(x[“年龄”]<25)和(x[“工资”]<40),axis=1)
或者为了获得更好的性能,请使用np.where 在np中,您可以使用任意数量的条件

df['stat'] = np.where(df['age'] < 25,np.where(df['salary'] < 40,True,False),False)
df['stat']=np.where(df['age']<25,np.where(df['salary']<40,True,False),False)