質問編集履歴
2
書式の変更
title
CHANGED
File without changes
|
body
CHANGED
@@ -16,7 +16,11 @@
|
|
16
16
|
raise KeyError(key)
|
17
17
|
KeyError: 'reading score'```
|
18
18
|
|
19
|
+
|
20
|
+
|
21
|
+
```ここに言語を入力
|
22
|
+
コード
|
19
|
-
```# システム関係のコマンドライブラリ
|
23
|
+
``````# システム関係のコマンドライブラリ
|
20
24
|
import sys
|
21
25
|
|
22
26
|
# データベースライブラリ pandas
|
@@ -78,9 +82,11 @@
|
|
78
82
|
|
79
83
|
|
80
84
|
|
85
|
+
|
81
86
|
```
|
82
87
|
|
83
88
|
|
89
|
+
|
84
90
|
### 試したこと
|
85
91
|
csvファイルのcolumsをpythonにそのままコピペしたり、名前を変えてみたりしました。
|
86
92
|
|
1
書式の改善
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
kaggle
|
1
|
+
kaggle[リンク内容](https://www.kaggle.com/spscientist/students-performance-in-exams)のデータを用いて、X軸がtest preparation courseでグループ分けしたもの、y軸がmath・reading・writingのテストの総合的な平均点となっている棒グラフを作りたいと考えております。
|
2
2
|
★9月からの大学の授業で初めてpythonに触れたのでまだ分からない所だらけです。
|
3
3
|
質問部分の他に3列まとめて平均値を出す部分も自分なりに調べましたが、あっているかどうか自信がありません。他にコードがおかしいよという所があれば教えてください。
|
4
4
|
|
@@ -6,7 +6,7 @@
|
|
6
6
|
### 発生している問題・エラーメッセージ
|
7
7
|
Key Errorが出てしまう。
|
8
8
|
|
9
|
-
|
9
|
+
```
|
10
10
|
mean_df= df['math score']['reading score']['writing score'].mean()
|
11
11
|
File "/opt/anaconda3/envs/Kivy/lib/python3.6/site-packages/pandas/core/series.py", line 882, in __getitem__
|
12
12
|
return self._get_value(key)
|
@@ -14,39 +14,73 @@
|
|
14
14
|
loc = self.index.get_loc(label)
|
15
15
|
File "/opt/anaconda3/envs/Kivy/lib/python3.6/site-packages/pandas/core/indexes/range.py", line 349, in get_loc
|
16
16
|
raise KeyError(key)
|
17
|
-
KeyError: 'reading score'
|
17
|
+
KeyError: 'reading score'```
|
18
|
-
### 該当のソースコード
|
19
18
|
|
19
|
+
```# システム関係のコマンドライブラリ
|
20
20
|
import sys
|
21
21
|
|
22
|
+
# データベースライブラリ pandas
|
22
23
|
import pandas as pd
|
23
24
|
|
25
|
+
# 計算用のライブラリ numpy
|
24
26
|
import numpy as np
|
25
27
|
|
28
|
+
# 作図ライブラリ matplotlib
|
26
29
|
import matplotlib.pyplot as plt
|
27
30
|
|
31
|
+
# 日本語フォントを設定するためのライブラリ
|
28
32
|
from matplotlib.font_manager import FontProperties
|
29
33
|
fp = FontProperties(fname=r'./fonts/HuiFontP29.ttf', size=16)
|
30
34
|
|
35
|
+
|
36
|
+
# データを読み込む
|
31
37
|
try:
|
32
38
|
df = pd.read_csv('./data/students.csv',engine="python")
|
33
39
|
except:
|
34
40
|
print('Data File Read Error: Exit')
|
35
41
|
sys.exit()
|
36
42
|
|
43
|
+
# figureオブジェクトを生成する
|
37
44
|
fig = plt.figure()
|
38
45
|
|
46
|
+
# axesオブジェクトをfigureオブジェクトに設定する
|
39
47
|
ax = fig.add_subplot(1, 1, 1)
|
40
48
|
|
49
|
+
# テスト前の準備の度合いごとの平均値を計算する。
|
41
50
|
left = df.groupby('test preparation course').mean()
|
42
51
|
|
52
|
+
# 3つのテストの平均
|
43
53
|
mean_df= df['math score']['reading score']['writing score'].mean()
|
44
54
|
height = mean_df
|
45
55
|
|
56
|
+
# axesオブジェクトに対して棒グラフを設定する
|
46
57
|
ax.bar(left,height,width=0.5,color='mediumseagreen', edgecolor='darkgreen',ecolor='orange', capsize=5)
|
58
|
+
|
59
|
+
# axesオブジェクトに対して凡例設定
|
60
|
+
# ax.legend(["sample data"])
|
61
|
+
|
62
|
+
# axesオブジェクトに対してタイトルを設定
|
63
|
+
ax.set_title("テスト前準備の有無別の平均点", fontproperties=fp)
|
47
64
|
|
65
|
+
# axesオブジェクトに対してグリッドを設定
|
66
|
+
#ax.grid(True)
|
67
|
+
|
68
|
+
# axesオブジェクトに対して横軸名ラベルと縦軸名ラベルを設定
|
69
|
+
ax.set_xlabel('テスト前準備の有無', fontproperties=fp)
|
70
|
+
ax.set_ylabel('3つのテストの平均得点', fontproperties=fp)
|
71
|
+
|
72
|
+
# axesオブジェクトに対して横軸,縦軸の範囲指定
|
73
|
+
#ax.set_xlim(left= -2, right=40)
|
74
|
+
#ax.set_ylim(bottom=0, top=11)
|
75
|
+
|
76
|
+
# 表示する
|
48
77
|
plt.show()
|
49
78
|
|
79
|
+
|
80
|
+
|
81
|
+
```
|
82
|
+
|
83
|
+
|
50
84
|
### 試したこと
|
51
85
|
csvファイルのcolumsをpythonにそのままコピペしたり、名前を変えてみたりしました。
|
52
86
|
|