teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

7

テキスト修正

2020/02/21 14:54

投稿

jun68ykt
jun68ykt

スコア9058

answer CHANGED
@@ -8,7 +8,7 @@
8
8
   
9
9
  - `re` は[正規表現モジュール](https://docs.python.org/ja/3/library/re.html) です。
10
10
 
11
- ### 追記
11
+ ### 追記1
12
12
 
13
13
  正規表現を使う別の方法として、以下でもよいかと思います。
14
14
  ```python3
@@ -23,10 +23,13 @@
23
23
 
24
24
  ### 追記2
25
25
 
26
- 所与のデータフレームの`時刻`列には、日時として満たすべき形式の文字列が入っていることを前提とすれば、記の `before` は以下のような簡単なもので事足ります。
26
+ 所与のデータフレームの`時刻`列には、日時として満たすべき形式の文字列が入っていることを前提とすれば、1コードの `before` は以下のような簡単なもので事足ります。
27
27
 
28
28
  ```python3
29
29
  before = r'^(.+)(.)(.+)$'
30
+ ```
31
+ この場合、`after` は以下のように `\1` と `\2` をスペースなしでつなげます。
32
+ ```python3
30
33
  after = r'\1\2'
31
34
  ```
32
35
  - 動作確認用Repl.it: [https://repl.it/@jun68ykt/Q2427263](https://repl.it/@jun68ykt/Q2427263)

6

テキスト修正

2020/02/21 14:54

投稿

jun68ykt
jun68ykt

スコア9058

answer CHANGED
@@ -10,7 +10,7 @@
10
10
 
11
11
  ### 追記
12
12
 
13
- または、以下でもよいかと思います。
13
+ 正規表現を使う別の方法として、以下でもよいかと思います。
14
14
  ```python3
15
15
  before = r'^(\d{4}/\d{2}/\d{2})([日月火水木金土])\s+(\d{2}:\d{2})$'
16
16
  after = r'\1 \2'
@@ -23,7 +23,7 @@
23
23
 
24
24
  ### 追記2
25
25
 
26
- 所与のデータフレームの`時刻`列には、日時として満たすべき形式の文字列が入っていることを前提とすれば、上記の `before` は以下のような簡単なものでもよかったです。
26
+ 所与のデータフレームの`時刻`列には、日時として満たすべき形式の文字列が入っていることを前提とすれば、上記の `before` は以下のような簡単なもので事足ります。
27
27
 
28
28
  ```python3
29
29
  before = r'^(.+)(.)(.+)$'

5

テキスト修正

2020/02/21 14:50

投稿

jun68ykt
jun68ykt

スコア9058

answer CHANGED
@@ -19,4 +19,14 @@
19
19
  ```
20
20
  - 動作確認用Repl.it: [https://repl.it/@jun68ykt/Q2427262](https://repl.it/@jun68ykt/Q2427262)
21
21
   
22
- - pandas.DataFrame.[replace](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.replace.html)
22
+ - pandas.DataFrame.[replace](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.replace.html)
23
+
24
+ ### 追記2
25
+
26
+ 所与のデータフレームの`時刻`列には、日時として満たすべき形式の文字列が入っていることを前提とすれば、上記の `before` は以下のような簡単なものでもよかったです。
27
+
28
+ ```python3
29
+ before = r'^(.+)(.)(.+)$'
30
+ after = r'\1\2'
31
+ ```
32
+ - 動作確認用Repl.it: [https://repl.it/@jun68ykt/Q2427263](https://repl.it/@jun68ykt/Q2427263)

4

テキスト修正

2020/02/21 14:27

投稿

jun68ykt
jun68ykt

スコア9058

answer CHANGED
@@ -4,6 +4,19 @@
4
4
  ```python3
5
5
  df.loc[:, '時刻'] = df['時刻'].map(lambda s: re.sub(r'(.)', '', s))
6
6
  ```
7
- - 動作確認用Repl.it: [https://repl.it/@jun68ykt/Q242726-2](https://repl.it/@jun68ykt/Q242726-2)
7
+ - 動作確認用Repl.it: [https://repl.it/@jun68ykt/Q2427261](https://repl.it/@jun68ykt/Q2427261)
8
+  
9
+ - `re` は[正規表現モジュール](https://docs.python.org/ja/3/library/re.html) です。
8
10
 
11
+ ### 追記
12
+
13
+ または、以下でもよいかと思います。
14
+ ```python3
15
+ before = r'^(\d{4}/\d{2}/\d{2})([日月火水木金土])\s+(\d{2}:\d{2})$'
16
+ after = r'\1 \2'
17
+
18
+ df.replace({'時刻': before}, {'時刻': after}, regex=True, inplace=True)
19
+ ```
20
+ - 動作確認用Repl.it: [https://repl.it/@jun68ykt/Q2427262](https://repl.it/@jun68ykt/Q2427262)
21
+  
9
- `re` [正規表現モジュール](https://docs.python.org/ja/3/library/re.html) です。
22
+ - pandas.DataFrame.[replace](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.replace.html)

3

テキスト修正

2020/02/21 09:39

投稿

jun68ykt
jun68ykt

スコア9058

answer CHANGED
@@ -1,6 +1,8 @@
1
+ こんにちは
2
+
1
- こんにちは。以下でどうでしょう?
3
+ 以下でどうでしょう?
2
4
  ```python3
3
- df['時刻'].map(lambda s: re.sub(r'(.)', '', s))
5
+ df.loc[:, '時刻'] = df['時刻'].map(lambda s: re.sub(r'(.)', '', s))
4
6
  ```
5
7
  - 動作確認用Repl.it: [https://repl.it/@jun68ykt/Q242726-2](https://repl.it/@jun68ykt/Q242726-2)
6
8
 

2

テキスト修正

2020/02/21 08:51

投稿

jun68ykt
jun68ykt

スコア9058

answer CHANGED
@@ -2,6 +2,6 @@
2
2
  ```python3
3
3
  df['時刻'].map(lambda s: re.sub(r'(.)', '', s))
4
4
  ```
5
- - 動作確認用Repl.it: [https://repl.it/@jun68ykt/Q242726](https://repl.it/@jun68ykt/Q242726)
5
+ - 動作確認用Repl.it: [https://repl.it/@jun68ykt/Q242726-2](https://repl.it/@jun68ykt/Q242726-2)
6
6
 
7
7
  `re` は[正規表現モジュール](https://docs.python.org/ja/3/library/re.html) です。

1

テキスト修正

2020/02/21 08:47

投稿

jun68ykt
jun68ykt

スコア9058

answer CHANGED
@@ -1,5 +1,7 @@
1
1
  こんにちは。以下でどうでしょう?
2
2
  ```python3
3
- ser = df['時刻'].map(lambda s: re.sub(r'(.)', '', s))
3
+ df['時刻'].map(lambda s: re.sub(r'(.)', '', s))
4
4
  ```
5
- - 動作確認用Repl.it: [https://repl.it/@jun68ykt/Q242726](https://repl.it/@jun68ykt/Q242726)
5
+ - 動作確認用Repl.it: [https://repl.it/@jun68ykt/Q242726](https://repl.it/@jun68ykt/Q242726)
6
+
7
+ `re` は[正規表現モジュール](https://docs.python.org/ja/3/library/re.html) です。