gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 07:58:09 24 4
gpt4 key购买 nike

我正在尝试根据字符串是否包含在另一列中在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com