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

2024-06-25 23:09:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图根据一个字符串是否包含在另一个列中,在pandas数据帧中创建一个新列。我在用np.选择基于这个post。下面是一个示例dataframe和一个创建新列的示例函数

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)

我得到的结果是

^{pr2}$

我想要的是

   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

我做错什么了?在


Tags: inadd示例dfnponescolumnone
1条回答
网友
1楼 · 发布于 2024-06-25 23:09:15

如果需要测试子字符串,请使用^{}

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

如果需要精确匹配,请使用^{}==

^{pr2}$
 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

相关问题 更多 >