dataframe 根据条件查找_根据if-else条件通过查找创建新的pandas dataframe列

I have a pandas dataframe and I need to create a new column based on an if-else condition. This question already came up here multiple times (e.g., Creating a new column based on if-elif-else condition).

However, I cannot apply the proposed solution, since I also need to look up values in a list in order to check the condition. I cannot do this with the proposed solution, because I am not sure how I can access my lookup-list in the external function. My lookup-list would need to be global, which I want to avoid. I have the feeling there should be a better way to do this.

Consider the following dataframe df:

letters

A

B

C

D

E

F

I also have a list which contains lookup values:

lookup = [C,D]

Now, I want to create a new column in my dataframe which contains 1 if the respective value is contained in lookup and 0 if the values is not in lookup.

The typical approach would be:

df.apply(helper, axis=1)

def helper(row):

if(row['letters'].isin(lookup)):

row['result'] = 1

else:

row['result'] = 0

However, I do not know how I can access lookup in helper() without making it global.

The result should look like this:

letters result

A 0

B 0

C 1

D 1

E 0

F 0

解决方案

I think here it's worth showing a couple methods, on a single line using np.where with a boolean mask generated from isin, isin will return a boolean Series where any rows contain any matches in your list:

In [71]:

lookup = ['C','D']

df['result'] = np.where(df['letters'].isin(lookup), 1, 0)

df

Out[71]:

letters result

0 A 0

1 B 0

2 C 1

3 D 1

4 E 0

5 F 0

here using 2 loc statements and using ~ to invert the mask:

In [72]:

df.loc[df['letters'].isin(lookup),'result'] = 1

df.loc[~df['letters'].isin(lookup),'result'] = 0

df

Out[72]:

letters result

0 A 0

1 B 0

2 C 1

3 D 1

4 E 0

5 F 0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值