[英]How to write a conditional on two variables (columns) in Pandas
I'm trying to count instances when there was no login, but there was a card-view, and create a new column with the count (or True). 我试图在没有登录但有卡片视图的情况下对实例进行计数,并使用计数(或True)创建一个新列。 I used the conditional statement below and got a key error.
我在下面使用了条件语句,并得到了一个关键错误。 Can someone help me figure out what's going on?
有人可以帮我弄清楚发生了什么吗?
import pandas as pd
import numpy as np
sample = pd.DataFrame({ 'Month' : pd.Categorical(["Jan", "Jan", "Feb", "Feb", "March","Apr", "May"]),
'Name' : pd.Categorical(["Peter", "Meg", "Peter", "Meg", "Meg","Lois", "Lois"]),
'Logins': [1, 1, 1, 1, 1, 1, 0],
'Card': [1, 1, 2, 2, 1, 2, 1]})
sample['LoginNoCard'] = sample['Logins'].where((sample['Logins'] == 0) & (sample['Card'] > 0), sample[1])
The solution I have here is creating a new Data Frame. 我这里的解决方案是创建一个新的数据框。 I'd like to create a new column using a conditional.
我想使用条件创建一个新列。 If Logins == 0 & Card > 0, then 0. If Logins > 0 and Card == 0, then 1. Else NaN.
如果登录名== 0且Card> 0,则为0。如果Logins> 0且Card == 0,则为1。
You can consider using nested np.where()
conditions for if Logins == 0 & Card > 0
, then 0
, if Logins > 0 and Card == 0
, then 1
, else NaN
. 您可以考虑使用嵌套
np.where()
条件,如果Logins == 0 & Card > 0
0
,则为0
,如果Logins > 0 and Card == 0
,则为1
,否则为NaN
。
In [81]: np.where(((sample['Logins']==0) & (sample['Card']>0)), 0,
np.where(((sample['Logins']>0) & (sample['Card']==0)), 1,
pd.np.nan))
Out[81]: array([ nan, nan, nan, nan, nan, nan, 0.])
To assign this to a column, you could 要将其分配给列,您可以
In [82]: sample['LoginNoCard'] = np.where(((sample['Logins']==0) & (sample['Card']>0)), 0,
np.where(((sample['Logins']>0) & (sample['Card']==0)), 1,
pd.np.nan))
In [83]: sample
Out[83]:
Card Logins Month Name LoginNoCard
0 1 1 Jan Peter NaN
1 1 1 Jan Meg NaN
2 2 1 Feb Peter NaN
3 2 1 Feb Meg NaN
4 1 1 March Meg NaN
5 2 1 Apr Lois NaN
6 1 0 May Lois 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.