如何在np.where()中将列表的元素作为条件? [英] How to have list's elements as a condition in np.where()?

查看:451
本文介绍了如何在np.where()中将列表的元素作为条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想基于另一列的值创建一个新列,在该列中,要对在新列中分配的值进行某些条件评估.

I want to create a new column based on the values of another column where there are certain conditions to be evaluated for the values assigned in the new column.

我阅读了一些问题和答案( Numpy np.where多个条件)涉及到np.where(),但无法推断出最佳的(有效的)Python方式.

I read a few questions and answers (Numpy np.where multiple condition) involving np.where() but was unable to deduce the best(efficient) Pythonic way.

示例数据帧为:

      period
0      JAN16 
1  YTD JAN16

我想在以下情况下为列period_type分配值: 如果周期以x开头(其中x是列表的任何元素-> ['JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG',' SEP","OCT","NOV","DEC"]),然后period_type ='month',否则,period_type = period.split(0)

I want to assign values to column period_type on the following condition: if period starts with x (where x is any element of a list -> ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC']), then period_type = 'month', else, period_type = period.split(0)

我希望数据框为:

      period period_type
0      JAN16       month
1      JAN16       YTD

我无法在代码中应用任何逻辑:

I am unable to apply any logic in my code whatsoever:

df.loc[df['c1'] == 'Value', 'c2'] = 10

或者:

df['c2'] = np.where(df.c1 == 8,'X',df.c3)

推荐答案

一种方法是使用

One way is using str.startswith to check which rows do start with any of the values in the list (it also accepts a tuple of strings), and np.where to set the rows in the new column to month or the actual row value splitted and taking the first value:

l = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 
     'AUG', 'SEP', 'OCT', 'NOV', 'DEC']

m = df.period.str.startswith(tuple(l))
df['period_type'] = np.where(m, 'month', df.period.str.split().str[0])
df.loc[~m, 'period'] = df.loc[~m, 'period'].str.split().str[1]

   period   period_type
0  JAN16       month
1  JAN16         YTD

这篇关于如何在np.where()中将列表的元素作为条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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