首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在np.select中使用字符串条件的问题

在np.select中使用字符串条件的问题
EN

Stack Overflow用户
提问于 2019-04-25 10:49:55
回答 1查看 2.9K关注 0票数 2

我试图根据一个字符串是否包含在另一个专栏中,在熊猫数据中创建一个新的列。我使用基于这个np.select的post。下面是一个示例dataframe和一个创建新列的示例函数

代码语言:javascript
复制
df=pd.DataFrame({'column':['one','ones','other','two','twos','others','three','threes']})

def add(df):

    conditions = [
        ('one' in df['column']),
        ('two' in df['column']),
        ('three' in df['column']),
        ('other' in df['column'])] 

    choices = [1, 2, 3, 0]
    df['Int'] = np.select(conditions, choices, default=0)

    return df

new_df=add(df)

我得到的输出是

代码语言:javascript
复制
   column  Int
0     one    0
1    ones    0
2   other    0
3     two    0
4    twos    0
5  others    0
6   three    0
7  threes    0

我想要的是

代码语言:javascript
复制
   column  Int
0     one    1
1    ones    1
2   other    0
3     two    2
4    twos    2
5  others    0
6   three    3
7  threes    3

我做错什么了?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-25 10:51:59

如果需要测试子字符串,请使用Series.str.contains

代码语言:javascript
复制
 conditions = [
        (df['column'].str.contains('one')),
        (df['column'].str.contains('two')),
        (df['column'].str.contains('three')),
        (df['column'].str.contains('other'))] 

如果需要精确匹配,请使用Series.eq==

代码语言:javascript
复制
 conditions = [
        (df['column'].eq('one')),
        (df['column'].eq('two')),
        (df['column'].eq('three')),
        (df['column'].eq('other'))] 
代码语言:javascript
复制
 conditions = [
        (df['column'] == 'one'),
        (df['column'] == 'two'),
        (df['column'] == 'three'),
        (df['column'] == 'other')] 
代码语言:javascript
复制
print (new_df)
   column  Int
0     one    1
1    ones    1
2   other    0
3     two    2
4    twos    2
5  others    0
6   three    3
7  threes    3
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55847571

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档