【发布时间】:2020-07-10 20:57:54
【问题描述】:
我正在尝试在 Pandas DataFrame 中创建一个列,以显示(字符串)“Column1”是否包含“Column2”中的字符串。下面的可复制示例:
# Have
df = pd.DataFrame({'col1': ['a', 'aa', 'b', 'bb', 'c', 'cc'],
'col2': ['a', 'b', 'c', 'd', 'e', 'c']})
# Want: Series of 'does col1 contain col2?'
want: pd.Series([True, False, False, False, False, True])
# tried
tried = df.col1.str.contains(df.col2) # TypeError
我的错误是由于str.contains 想要在上面右侧的单个字符串,而不是另一个pd.Series。但我不确定如何解决这个问题......
【问题讨论】:
标签: python pandas string series