回答編集履歴

6

サンプルを更に追加

2020/04/28 10:32

投稿

magichan
magichan

スコア15898

test CHANGED
@@ -147,3 +147,43 @@
147
147
  ```
148
148
 
149
149
  のように書いても良いかもしれません。
150
+
151
+
152
+
153
+ ---
154
+
155
+ 【追記】
156
+
157
+
158
+
159
+ `value_counts().plot.bar()` で 描画するサンプル
160
+
161
+
162
+
163
+ ```Python
164
+
165
+ fig, axs = plt.subplots(3,2, figsize=(10,6))
166
+
167
+ df.index.year.value_counts(sort=False).plot.bar(ax=axs[0,1])
168
+
169
+ pos = {2016:(1,0), 2017:(1,1), 2018:(2,0), 2019:(2,1)}
170
+
171
+ for year, d in df.groupby(df.index.year):
172
+
173
+ ax = axs[pos[year][0], pos[year][1]]
174
+
175
+ d.index.month.value_counts(sort=False).plot.bar(ax=ax)
176
+
177
+ ax.set_title(year)
178
+
179
+ ax.set_xlabel("month")
180
+
181
+ ax.set_ylabel("number")
182
+
183
+
184
+
185
+ plt.tight_layout()
186
+
187
+ plt.show()
188
+
189
+ ```

5

マークダウンのミス修正

2020/04/28 10:32

投稿

magichan
magichan

スコア15898

test CHANGED
@@ -46,11 +46,13 @@
46
46
 
47
47
  - `read_csv()` のパラメータに`index_col` を渡して、上記 time行を Indexに設定
48
48
 
49
+
50
+
49
- しております。
51
+ しております。
50
52
 
51
53
 
52
54
 
53
- Indexにdatetime型のデータを渡して DatetimeIndex を設定するのは時系列データを扱う上でいろいろと便利なのでお勧めしておきます。
55
+ このように、Indexにdatetime型のデータを渡して DatetimeIndex を設定するのは時系列データを扱う上でいろいろと便利なのでお勧めしておきます。
54
56
 
55
57
 
56
58
 
@@ -84,7 +86,7 @@
84
86
 
85
87
 
86
88
 
87
- で、これを使ってグラフの描画部分を行うには
89
+ で、これを使ってグラフの描画部分を行うには、subplotの場所を指定するために更に enumerate を使って
88
90
 
89
91
 
90
92
 
@@ -144,4 +146,4 @@
144
146
 
145
147
  ```
146
148
 
147
- 方が良いかもしれません
149
+ ように書いても良いかもしれません

4

サンプルの間違い修正

2020/04/28 02:22

投稿

magichan
magichan

スコア15898

test CHANGED
@@ -126,7 +126,7 @@
126
126
 
127
127
  for year, d in df.groupby(df.index.year):
128
128
 
129
- ax = axs[*pos[year]]
129
+ ax = axs[pos[year][0], pos[year][1]]
130
130
 
131
131
  ax.hist(x=d.index.month, bins=range(1,12+1),alpha=0.5)
132
132
 

3

説明追加

2020/04/28 02:19

投稿

magichan
magichan

スコア15898

test CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
 
4
4
 
5
- 複数のCSVファイルから1つのDataFrameを作成するのはこんな感じに
5
+ 複数のCSVファイルから1つのDataFrameを作成するのはこんな感じになります。
6
6
 
7
7
 
8
8
 
@@ -40,21 +40,57 @@
40
40
 
41
41
 
42
42
 
43
+ 上記のサンプルでは、
44
+
43
- 上記のサンプルでは、`read_csv()` のパラメータに`parse_dates` を渡して、time行をdatetime型で読み込んでおります。
45
+ - `read_csv()` のパラメータに`parse_dates` を渡して、time行をdatetime型で読み込
46
+
47
+ - `read_csv()` のパラメータに`index_col` を渡して、上記 time行を Indexに設定
48
+
49
+ しております。
44
50
 
45
51
 
46
52
 
53
+ Indexにdatetime型のデータを渡して DatetimeIndex を設定するのは時系列データを扱う上でいろいろと便利なのでお勧めしておきます。
54
+
55
+
56
+
57
+ [https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#indexing
58
+
59
+ ](https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#indexing)
60
+
61
+
62
+
47
- 描画部分
63
+ 、上記のように纏められた DataFrame から年毎の処理を行うに、``groupby()`` を使ってループ処理を行うことで
48
64
 
49
65
 
50
66
 
51
67
  ```Python
52
68
 
69
+ for year, d in df.groupby(df.index.year):
70
+
71
+ # 何かしらの処理
72
+
73
+ print(f"{year} 年のデータ")
74
+
75
+ print(d)
76
+
77
+ ```
53
78
 
54
79
 
80
+
55
- fig, axs = plt.subplots(3,2, figsize=(10,6))
81
+ のように、ループ毎に year とそのデータを得ることが出来ます。
56
82
 
57
83
 
84
+
85
+
86
+
87
+ で、これを使ってグラフの描画部分を行うには
88
+
89
+
90
+
91
+ ```Python
92
+
93
+ fig, axs = plt.subplots(3,2, figsize=(10,6))
58
94
 
59
95
  for p, (year, d) in enumerate(df.groupby(df.index.year)):
60
96
 
@@ -77,3 +113,35 @@
77
113
  ```
78
114
 
79
115
  こんな感じになるのではないでしょうか。
116
+
117
+
118
+
119
+ `enumerate` の部分が気に入らないのであれば dictを準備して
120
+
121
+ ```Python
122
+
123
+ fig, axs = plt.subplots(3,2, figsize=(10,6))
124
+
125
+ pos = {2016:(1,0), 2017:(1,1), 2018:(2,0), 2019:(2,1)}
126
+
127
+ for year, d in df.groupby(df.index.year):
128
+
129
+ ax = axs[*pos[year]]
130
+
131
+ ax.hist(x=d.index.month, bins=range(1,12+1),alpha=0.5)
132
+
133
+ ax.set_title(year)
134
+
135
+ ax.set_xlabel("month")
136
+
137
+ ax.set_ylabel("number")
138
+
139
+
140
+
141
+ plt.tight_layout()
142
+
143
+ plt.show()
144
+
145
+ ```
146
+
147
+ の方が良いかもしれません

2

説明追加

2020/04/28 02:12

投稿

magichan
magichan

スコア15898

test CHANGED
File without changes

1

サンプル追加

2020/04/28 02:12

投稿

magichan
magichan

スコア15898

test CHANGED
@@ -40,6 +40,40 @@
40
40
 
41
41
 
42
42
 
43
- 上記のサンプルでは、
43
+ 上記のサンプルでは、`read_csv()` のパラメータに`parse_dates` を渡して、time行をdatetime型で読み込んでおります。
44
44
 
45
+
46
+
47
+ で描画部分は
48
+
49
+
50
+
51
+ ```Python
52
+
53
+
54
+
55
+ fig, axs = plt.subplots(3,2, figsize=(10,6))
56
+
57
+
58
+
45
- `read_csv()` のパラメータに`parse_dates` を渡して、time行をdatetime型で読み込んでいる
59
+ for p, (year, d) in enumerate(df.groupby(df.index.year)):
60
+
61
+ ax = axs[(p+2)//2, (p+2)%2]
62
+
63
+ ax.hist(x=d.index.month, bins=range(1,12+1),alpha=0.5)
64
+
65
+ ax.set_title(year)
66
+
67
+ ax.set_xlabel("month")
68
+
69
+ ax.set_ylabel("number")
70
+
71
+
72
+
73
+ plt.tight_layout()
74
+
75
+ plt.show()
76
+
77
+ ```
78
+
79
+ こんな感じになるのではないでしょうか。