python - 在 np.select 中使用字符串条件时出现问题

标签 python pandas numpy

我正在尝试根据字符串是否包含在另一列中在 pandas 数据框中创建一个新列。我正在使用基于此的 np.select post 。这是一个示例数据框和一个用于创建新列的示例函数

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)

我得到的输出是

   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

我想要的是

   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

我做错了什么?

最佳答案

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

 conditions = [
        (df['column'].str.contains('one')),
        (df['column'].str.contains('two')),
        (df['column'].str.contains('three')),
        (df['column'].str.contains('other'))] 

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

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

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

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

关于python - 在 np.select 中使用字符串条件时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55847571/

相关文章:

python - 如何将子列表转换为列表

python - Scrapy爬虫没有返回预期的html

python - pandas 数据框中行中的唯一文本

python - 使用公共(public)键将带有公共(public)键作为元素的字典的 Pandas Dataframe 列转换为单独的数据框

python - 具有 NA 的两列的条件最小值

python - Numpy "Where"函数无法避免 evaluate Sqrt(negative)

python - Numpy 将一维数组打印为列

python - 将 Numpy 方阵数组转换为 numpy 方阵,保留 "layout"

python - 构造结构化数组时元组和列表有什么区别?

python - cython中融合类型的替代品