回答編集履歴

2

修正

2020/11/02 09:40

投稿

tiitoi
tiitoi

スコア21956

test CHANGED
@@ -16,4 +16,6 @@
16
16
 
17
17
  df.to_csv("output.csv")
18
18
 
19
+ print(df)
20
+
19
21
  ```

1

修正

2020/11/02 09:40

投稿

tiitoi
tiitoi

スコア21956

test CHANGED
@@ -1,91 +1,19 @@
1
- 1列目が日付だと仮定すると、read_csv() で読み込んだあとに `df[(start < df[0]) & (df[0] <= end)]`指定範囲の日付の行だけ抽出すればよいです。
1
+ 1列目が日付だと仮定すると、read_csv() で読み込んだあとに `df[0] <= 日付を表す文字列` が成り立つ行は40、そうでない行は20を numpy.where() で代入すればよいです。
2
2
 
3
3
 
4
4
 
5
5
  ```python
6
6
 
7
+ import numpy as np
8
+
7
- df = pd.read_csv("sample.csv", header=None, parse_dates=[0])
9
+ import pandas as pd
8
10
 
9
11
 
10
12
 
11
- start = "2020/10/10"
13
+ df = pd.read_csv("sample.csv", header=None, parse_dates=[0])
12
14
 
13
- end = "2020/10/20"
15
+ df[2] = np.where(df[0] <= "2020-06-20T14:17:34", 40, 20)
14
16
 
15
-
16
-
17
- df = df[(start < df[0]) & (df[0] <= end)]
17
+ df.to_csv("output.csv")
18
-
19
- print(df)
20
18
 
21
19
  ```
22
-
23
-
24
-
25
- csv のサンプル
26
-
27
-
28
-
29
- ```
30
-
31
- 2020-10-01,0
32
-
33
- 2020-10-02,1
34
-
35
- 2020-10-03,2
36
-
37
- 2020-10-04,3
38
-
39
- 2020-10-05,4
40
-
41
- 2020-10-06,5
42
-
43
- 2020-10-07,6
44
-
45
- 2020-10-08,7
46
-
47
- 2020-10-09,8
48
-
49
- 2020-10-10,9
50
-
51
- 2020-10-11,10
52
-
53
- 2020-10-12,11
54
-
55
- 2020-10-13,12
56
-
57
- 2020-10-14,13
58
-
59
- 2020-10-15,14
60
-
61
- 2020-10-16,15
62
-
63
- 2020-10-17,16
64
-
65
- 2020-10-18,17
66
-
67
- 2020-10-19,18
68
-
69
- 2020-10-20,19
70
-
71
- 2020-10-21,20
72
-
73
- 2020-10-22,21
74
-
75
- 2020-10-23,22
76
-
77
- 2020-10-24,23
78
-
79
- 2020-10-25,24
80
-
81
- 2020-10-26,25
82
-
83
- 2020-10-27,26
84
-
85
- 2020-10-28,27
86
-
87
- 2020-10-29,28
88
-
89
- 2020-10-30,29
90
-
91
- ```