我试图根据一个字符串是否包含在另一个列中,在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
我做错什么了?在
如果需要测试子字符串,请使用^{} :
如果需要精确匹配,请使用^{} 或
^{pr2}$==
:相关问题 更多 >
编程相关推荐