2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

str.containsで複数の式を利用

Posted at

#概要#
str.containsで「or」といった複数の条件を設定する際には正規表現を利用

#実装#
データフレームの作成

import pandas as pd

dict1=dict(date=[20151204,20151204,20151204],prefecture=["東京都","神奈川県","和歌山県"])
list1=pd.DataFrame(data=dict1)
list1

| date | prefecture |
|:-:|:-:|:-:|:-:|
|20151204| 東京都 |True|
|20151204| 神奈川県| True|
|20151204| 大阪府|False|

都道府県から任意の地方に該当するかについてstr.containsの複数の式を利用

list1['関東']= list1['prefecture'].str.contains('神奈川県|東京都')
list1

| date | prefecture | 関東|
|:-:|:-:|:-:|:-:|
|20151204| 東京都 |True|
|20151204| 神奈川県| True|
|20151204| 大阪府|False|

正規表現を使わないと以下のようになってしまいます。

list1['関東']= list1['prefecture'].str.contains('東京都' or '神奈川県')
list1

| date | prefecture | 関東|
|:-:|:-:|:-:|:-:|
|20151204| 東京都 |True|
|20151204| 神奈川県| False|
|20151204| 大阪府|False|

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?