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

回答編集履歴

6

サンプルを更に追加

2020/04/28 10:32

投稿

magichan
magichan

スコア15898

answer CHANGED
@@ -72,4 +72,24 @@
72
72
  plt.tight_layout()
73
73
  plt.show()
74
74
  ```
75
- のように書いても良いかもしれません。
75
+ のように書いても良いかもしれません。
76
+
77
+ ---
78
+ 【追記】
79
+
80
+ `value_counts().plot.bar()` で 描画するサンプル
81
+
82
+ ```Python
83
+ fig, axs = plt.subplots(3,2, figsize=(10,6))
84
+ df.index.year.value_counts(sort=False).plot.bar(ax=axs[0,1])
85
+ pos = {2016:(1,0), 2017:(1,1), 2018:(2,0), 2019:(2,1)}
86
+ for year, d in df.groupby(df.index.year):
87
+ ax = axs[pos[year][0], pos[year][1]]
88
+ d.index.month.value_counts(sort=False).plot.bar(ax=ax)
89
+ ax.set_title(year)
90
+ ax.set_xlabel("month")
91
+ ax.set_ylabel("number")
92
+
93
+ plt.tight_layout()
94
+ plt.show()
95
+ ```

5

マークダウンのミス修正

2020/04/28 10:32

投稿

magichan
magichan

スコア15898

answer CHANGED
@@ -22,10 +22,11 @@
22
22
  上記のサンプルでは、
23
23
  - `read_csv()` のパラメータに`parse_dates` を渡して、time行をdatetime型で読み込む
24
24
  - `read_csv()` のパラメータに`index_col` を渡して、上記 time行を Indexに設定
25
- しております。
26
25
 
27
- Indexにdatetime型のデータして DatetimeIndex を設定するのは時系列データを扱う上でいろいろと便利なので勧めしておきます。
26
+ をしておます。
28
27
 
28
+ このように、Indexにdatetime型のデータを渡して DatetimeIndex を設定するのは時系列データを扱う上でいろいろと便利なのでお勧めしておきます。
29
+
29
30
  [https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#indexing
30
31
  ](https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#indexing)
31
32
 
@@ -41,7 +42,7 @@
41
42
  のように、ループ毎に year とそのデータを得ることが出来ます。
42
43
 
43
44
 
44
- で、これを使ってグラフの描画部分を行うには
45
+ で、これを使ってグラフの描画部分を行うには、subplotの場所を指定するために更に enumerate を使って
45
46
 
46
47
  ```Python
47
48
  fig, axs = plt.subplots(3,2, figsize=(10,6))
@@ -71,4 +72,4 @@
71
72
  plt.tight_layout()
72
73
  plt.show()
73
74
  ```
74
- 方が良いかもしれません
75
+ ように書いても良いかもしれません

4

サンプルの間違い修正

2020/04/28 02:22

投稿

magichan
magichan

スコア15898

answer CHANGED
@@ -62,7 +62,7 @@
62
62
  fig, axs = plt.subplots(3,2, figsize=(10,6))
63
63
  pos = {2016:(1,0), 2017:(1,1), 2018:(2,0), 2019:(2,1)}
64
64
  for year, d in df.groupby(df.index.year):
65
- ax = axs[*pos[year]]
65
+ ax = axs[pos[year][0], pos[year][1]]
66
66
  ax.hist(x=d.index.month, bins=range(1,12+1),alpha=0.5)
67
67
  ax.set_title(year)
68
68
  ax.set_xlabel("month")

3

説明追加

2020/04/28 02:19

投稿

magichan
magichan

スコア15898

answer CHANGED
@@ -1,6 +1,6 @@
1
1
  まず複数のCSVファイルですが、各列に日時情報が入っているのであれば全てのデータをまとめて1つのDataFrameとして管理したほうが楽です。(データ量があまりにも大きくてメモリを圧迫する場合は除きますが)
2
2
 
3
- 複数のCSVファイルから1つのDataFrameを作成するのはこんな感じに
3
+ 複数のCSVファイルから1つのDataFrameを作成するのはこんな感じになります。
4
4
 
5
5
  ```Python
6
6
  import pandas as pd
@@ -19,14 +19,32 @@
19
19
  print(df)
20
20
  ```
21
21
 
22
+ 上記のサンプルでは、
22
- 上記のサンプルでは、`read_csv()` のパラメータに`parse_dates` を渡して、time行をdatetime型で読み込んでおります。
23
+ - `read_csv()` のパラメータに`parse_dates` を渡して、time行をdatetime型で読み込
24
+ - `read_csv()` のパラメータに`index_col` を渡して、上記 time行を Indexに設定
25
+ しております。
23
26
 
24
- で描画部分
27
+ Indexにdatetime型のデータを渡して DatetimeIndex を設定するの時系列データを扱う上でいろいろと便利なのでお勧めしておきます。
25
28
 
29
+ [https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#indexing
30
+ ](https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#indexing)
31
+
32
+ で、上記のように纏められた DataFrame から年毎の処理を行うには、``groupby()`` を使ってループ処理を行うことで
33
+
26
34
  ```Python
35
+ for year, d in df.groupby(df.index.year):
36
+ # 何かしらの処理
37
+ print(f"{year} 年のデータ")
38
+ print(d)
39
+ ```
27
40
 
41
+ のように、ループ毎に year とそのデータを得ることが出来ます。
42
+
43
+
44
+ で、これを使ってグラフの描画部分を行うには
45
+
46
+ ```Python
28
47
  fig, axs = plt.subplots(3,2, figsize=(10,6))
29
-
30
48
  for p, (year, d) in enumerate(df.groupby(df.index.year)):
31
49
  ax = axs[(p+2)//2, (p+2)%2]
32
50
  ax.hist(x=d.index.month, bins=range(1,12+1),alpha=0.5)
@@ -37,4 +55,20 @@
37
55
  plt.tight_layout()
38
56
  plt.show()
39
57
  ```
40
- こんな感じになるのではないでしょうか。
58
+ こんな感じになるのではないでしょうか。
59
+
60
+ `enumerate` の部分が気に入らないのであれば dictを準備して
61
+ ```Python
62
+ fig, axs = plt.subplots(3,2, figsize=(10,6))
63
+ pos = {2016:(1,0), 2017:(1,1), 2018:(2,0), 2019:(2,1)}
64
+ for year, d in df.groupby(df.index.year):
65
+ ax = axs[*pos[year]]
66
+ ax.hist(x=d.index.month, bins=range(1,12+1),alpha=0.5)
67
+ ax.set_title(year)
68
+ ax.set_xlabel("month")
69
+ ax.set_ylabel("number")
70
+
71
+ plt.tight_layout()
72
+ plt.show()
73
+ ```
74
+ の方が良いかもしれません

2

説明追加

2020/04/28 02:12

投稿

magichan
magichan

スコア15898

answer CHANGED
File without changes

1

サンプル追加

2020/04/28 02:12

投稿

magichan
magichan

スコア15898

answer CHANGED
@@ -19,5 +19,22 @@
19
19
  print(df)
20
20
  ```
21
21
 
22
- 上記のサンプルでは、
23
- `read_csv()` のパラメータに`parse_dates` を渡して、time行をdatetime型で読み込んでいる
22
+ 上記のサンプルでは、`read_csv()` のパラメータに`parse_dates` を渡して、time行をdatetime型で読み込んでおります。
23
+
24
+ で描画部分は
25
+
26
+ ```Python
27
+
28
+ fig, axs = plt.subplots(3,2, figsize=(10,6))
29
+
30
+ for p, (year, d) in enumerate(df.groupby(df.index.year)):
31
+ ax = axs[(p+2)//2, (p+2)%2]
32
+ ax.hist(x=d.index.month, bins=range(1,12+1),alpha=0.5)
33
+ ax.set_title(year)
34
+ ax.set_xlabel("month")
35
+ ax.set_ylabel("number")
36
+
37
+ plt.tight_layout()
38
+ plt.show()
39
+ ```
40
+ こんな感じになるのではないでしょうか。