回答編集履歴

1

うまくいったコード全体を示す

2018/07/14 03:50

投稿

KojiDoi
KojiDoi

スコア13671

test CHANGED
@@ -12,6 +12,118 @@
12
12
 
13
13
  ]
14
14
 
15
+ ```
16
+
17
+
18
+
19
+ 追記: datetime型をサポートしうまくいったコード全体を示します。
20
+
21
+ ```
22
+
23
+ #!/usr/bin/env python3
24
+
25
+ import pandas as pd
26
+
27
+ import io
28
+
29
+
30
+
31
+ df1 = pd.read_table(io.StringIO("""
32
+
33
+ 開催日 レース
34
+
35
+ 0 2017-01-28 1
36
+
37
+ 0 2017-01-28 2
38
+
39
+ 0 2017-01-28 3
40
+
41
+ 0 2017-01-28 4
42
+
43
+ 0 2017-01-28 5
44
+
45
+ 0 2017-01-28 6
46
+
47
+ 0 2017-01-28 7
48
+
49
+ 0 2017-01-28 8
50
+
51
+ 0 2017-01-28 9
52
+
53
+ 0 2017-01-28 10
54
+
55
+ 0 2017-01-28 11
56
+
57
+ 0 2017-01-28 12
58
+
59
+ 0 2017-01-29 1
60
+
61
+ 0 2017-01-29 2
62
+
63
+ 0 2017-01-29 3
64
+
65
+ 0 2017-01-29 4
66
+
67
+ 0 2017-01-29 5
68
+
69
+ 0 2017-01-29 6
70
+
71
+ 0 2017-01-29 7
72
+
73
+ 0 2017-01-29 8
74
+
75
+ 0 2017-01-29 9
76
+
77
+ 0 2017-01-29 10
78
+
79
+ 0 2017-01-29 11
80
+
81
+ 0 2017-01-29 12
82
+
83
+ 0 2017-01-30 1
84
+
85
+ 0 2017-01-30 2
86
+
87
+ 0 2017-01-30 3
88
+
89
+ 0 2017-01-30 4
90
+
91
+ 0 2017-01-30 5
92
+
93
+ 0 2017-01-30 6
94
+
95
+ 0 2017-01-30 7
96
+
97
+ 0 2017-01-30 8
98
+
99
+ 0 2017-01-30 9
100
+
101
+ 0 2017-01-30 10
102
+
103
+ 0 2017-01-30 11
104
+
105
+ 0 2017-01-30 12
106
+
107
+ """), delim_whitespace=True
108
+
109
+ )
110
+
111
+ df1["開催日"] = pd.to_datetime(df1["開催日"])
112
+
113
+
114
+
115
+ df2 = df1[
116
+
117
+ (df1.apply(lambda x: "{} {:02}".format(x["開催日"], x["レース"]), axis=1)>="2017-01-28 00:00:00 02") &
118
+
119
+ (df1.apply(lambda x: "{} {:02}".format(x["開催日"], x["レース"]), axis=1)<="2017-01-29 00:00:00 05")
120
+
121
+ ]
122
+
123
+ print(df2)
124
+
125
+ ```
126
+
15
127
 
16
128
 
17
129
  ```