下記の時、+SのみがあるところをTrueとする列を作成したいです。
しかし、str.contains('+S')とすると、エラーが生じ、また、str.contains('S')とすると、2SまでもTrueになってしまいます。
どなたか教えて頂けると幸いです。
python
1import pandas as pd 2 3df = pd.DataFrame({'room': ['+S', '+S', '2S']}) 4df['+'] = df['room'].str.contains('+S')
error
1--------------------------------------------------------------------------- 2error Traceback (most recent call last) 3<ipython-input-4-7266d58e48f7> in <module>() 4----> 1 df['+'] = df['room'].str.contains('+S') 5 67 frames 7/usr/lib/python3.6/sre_parse.py in _parse(source, state, verbose, nested, first) 8 614 if not item or (_len(item) == 1 and item[0][0] is AT): 9 615 raise source.error("nothing to repeat", 10--> 616 source.tell() - here + len(this)) 11 617 if item[0][0] in _REPEATCODES: 12 618 raise source.error("multiple repeat", 13 14error: nothing to repeat at position 0
他に自身が試したコード
df['+'] = df['room'].str.contains('S') df.head()
得られた結果。しかし、欲しい結果ではない。
room + 0 +S True 1 +S True 2 2S True
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/07/14 05:47