質問編集履歴

3

誤字

2020/07/07 08:10

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,117 +1,3 @@
1
- ### 前提・実現したいこと
1
+ ----------------------
2
2
 
3
-
4
-
5
- csvファイルを読み込み、そこからpandasにおけるgroupby()メソッドを利用して国のコードが"JP"のみのデータの平均値をとり、それをグラフ化したいのですがgroupbyを使用するところでエラーが発生しました。
6
-
7
- どなたか原因を教えていただきたいです。
8
-
9
-
10
-
11
- ### 発生している問題・エラーメッセージ
3
+ もう一度考え直たいので白紙にさせくださ
12
-
13
-
14
-
15
- ```
16
-
17
- KeyError Traceback (most recent call last)
18
-
19
- <ipython-input-10-5bf405663c72> in <module>()
20
-
21
- 20
22
-
23
- 21 import plotly_express as px
24
-
25
- ---> 22 df_melt = df.melt(id_vars='date', value_vars=["小売・娯楽", "食料品店・薬局", "公園", "乗換駅", "職場", "住宅"])
26
-
27
- 23 fig = px.line(df_melt, x="date", y="value", color='variable',
28
-
29
- 24 title="Google Mobility Report")
30
-
31
-
32
-
33
- 7 frames
34
-
35
- /usr/local/lib/python3.6/dist-packages/pandas/core/indexing.py in _validate_read_indexer(self, key, indexer, axis, raise_missing)
36
-
37
- 1653 if not (ax.is_categorical() or ax.is_interval()):
38
-
39
- 1654 raise KeyError(
40
-
41
- -> 1655 "Passing list-likes to .loc or [] with any missing labels "
42
-
43
- 1656 "is no longer supported, see "
44
-
45
- 1657 "https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#deprecate-loc-reindex-listlike" # noqa:E501
46
-
47
-
48
-
49
- KeyError: 'Passing list-likes to .loc or [] with any missing labels is no longer supported, see https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#deprecate-loc-reindex-listlike'
50
-
51
-
52
-
53
- ```
54
-
55
-
56
-
57
- ### 該当のソースコード
58
-
59
-
60
-
61
- ```Python
62
-
63
- import pandas as pd
64
-
65
-
66
-
67
- # Google's COVID-19 Community Mobility Reports (COVID-19 コミュニティ モビリティ レポート) の読み込み
68
-
69
- df = pd.read_csv("/content/drive/My Drive/Global_Mobility_Report.csv", encoding="utf-8")
70
-
71
-
72
-
73
- df = df.rename(columns={'retail_and_recreation_percent_change_from_baseline':'小売・娯楽',
74
-
75
- 'grocery_and_pharmacy_percent_change_from_baseline': '食料品店・薬局',
76
-
77
- 'parks_percent_change_from_baseline': '公園',
78
-
79
- 'transit_stations_percent_change_from_baseline': '乗換駅',
80
-
81
- 'workplaces_percent_change_from_baseline': '職場',
82
-
83
- 'residential_percent_change_from_baseline': '住宅'})
84
-
85
-
86
-
87
- # 日本のデータのみ絞り込む (# データ数 = 6240件)
88
-
89
-
90
-
91
- #print(df.info())
92
-
93
-
94
-
95
- grp = df.groupby('country_region_code').mean()
96
-
97
- df = grp.query('country_region_code == "JP"')
98
-
99
-
100
-
101
-
102
-
103
- import plotly_express as px
104
-
105
- df_melt = df.melt(id_vars='date', value_vars=["小売・娯楽", "食料品店・薬局", "公園", "乗換駅", "職場", "住宅"])
106
-
107
- fig = px.line(df_melt, x="date", y="value", color='variable',
108
-
109
- title="Google Mobility Report")
110
-
111
-
112
-
113
-
114
-
115
- fig.show()
116
-
117
- ```

2

文法の修正

2020/07/07 08:10

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -14,21 +14,41 @@
14
14
 
15
15
  ```
16
16
 
17
- /usr/local/lib/python3.6/dist-packages/pandas/core/groupby/grouper.py in get_grouper(obj, key, axis, level, sort, observed, mutated, validate)
17
+ KeyError Traceback (most recent call last)
18
18
 
19
- 598 in_axis, name, level, gpr = False, None, gpr, None
19
+ <ipython-input-10-5bf405663c72> in <module>()
20
20
 
21
- 599 else:
21
+ 20
22
22
 
23
- --> 600 raise KeyError(gpr)
23
+ 21 import plotly_express as px
24
24
 
25
- 601 elif isinstance(gpr, Grouper) and gpr.key is not None:
25
+ ---> 22 df_melt = df.melt(id_vars='date', value_vars=["小売・娯楽", "食料品店・薬局", "公園", "乗換駅", "職場", "住宅"])
26
26
 
27
+ 23 fig = px.line(df_melt, x="date", y="value", color='variable',
28
+
27
- 602 # Add key to exclusions
29
+ 24 title="Google Mobility Report")
28
30
 
29
31
 
30
32
 
33
+ 7 frames
34
+
35
+ /usr/local/lib/python3.6/dist-packages/pandas/core/indexing.py in _validate_read_indexer(self, key, indexer, axis, raise_missing)
36
+
37
+ 1653 if not (ax.is_categorical() or ax.is_interval()):
38
+
31
- KeyError: 'JP'
39
+ 1654 raise KeyError(
40
+
41
+ -> 1655 "Passing list-likes to .loc or [] with any missing labels "
42
+
43
+ 1656 "is no longer supported, see "
44
+
45
+ 1657 "https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#deprecate-loc-reindex-listlike" # noqa:E501
46
+
47
+
48
+
49
+ KeyError: 'Passing list-likes to .loc or [] with any missing labels is no longer supported, see https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#deprecate-loc-reindex-listlike'
50
+
51
+
32
52
 
33
53
  ```
34
54
 
@@ -66,11 +86,17 @@
66
86
 
67
87
  # 日本のデータのみ絞り込む (# データ数 = 6240件)
68
88
 
89
+
90
+
69
- print(df.info())
91
+ #print(df.info())
70
92
 
71
93
 
72
94
 
73
- df = df.groupby("JP").mean()
95
+ grp = df.groupby('country_region_code').mean()
96
+
97
+ df = grp.query('country_region_code == "JP"')
98
+
99
+
74
100
 
75
101
 
76
102
 
@@ -89,39 +115,3 @@
89
115
  fig.show()
90
116
 
91
117
  ```
92
-
93
-
94
-
95
- ###実行結果
96
-
97
- Data columns (total 13 columns):
98
-
99
- Column Non-Null Count Dtype
100
-
101
- --- ------ -------------- -----
102
-
103
- 0 country_region_code 605422 non-null object
104
-
105
- 1 country_region 606467 non-null object
106
-
107
- 2 sub_region_1 588497 non-null object
108
-
109
- 3 sub_region_2 350398 non-null object
110
-
111
- 4 iso_3166_2_code 220137 non-null object
112
-
113
- 5 census_fips_code 350532 non-null float64
114
-
115
- 6 date 606467 non-null object
116
-
117
- 7 小売・娯楽 464961 non-null float64
118
-
119
- 8 食料品店・薬局 445097 non-null float64
120
-
121
- 9 公園 276593 non-null float64
122
-
123
- 10 乗換駅 322793 non-null float64
124
-
125
- 11 職場 592165 non-null float64
126
-
127
- 12 住宅 336291 non-null float64

1

追記依頼の実行

2020/07/04 23:02

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -66,6 +66,8 @@
66
66
 
67
67
  # 日本のデータのみ絞り込む (# データ数 = 6240件)
68
68
 
69
+ print(df.info())
70
+
69
71
 
70
72
 
71
73
  df = df.groupby("JP").mean()
@@ -87,3 +89,39 @@
87
89
  fig.show()
88
90
 
89
91
  ```
92
+
93
+
94
+
95
+ ###実行結果
96
+
97
+ Data columns (total 13 columns):
98
+
99
+ Column Non-Null Count Dtype
100
+
101
+ --- ------ -------------- -----
102
+
103
+ 0 country_region_code 605422 non-null object
104
+
105
+ 1 country_region 606467 non-null object
106
+
107
+ 2 sub_region_1 588497 non-null object
108
+
109
+ 3 sub_region_2 350398 non-null object
110
+
111
+ 4 iso_3166_2_code 220137 non-null object
112
+
113
+ 5 census_fips_code 350532 non-null float64
114
+
115
+ 6 date 606467 non-null object
116
+
117
+ 7 小売・娯楽 464961 non-null float64
118
+
119
+ 8 食料品店・薬局 445097 non-null float64
120
+
121
+ 9 公園 276593 non-null float64
122
+
123
+ 10 乗換駅 322793 non-null float64
124
+
125
+ 11 職場 592165 non-null float64
126
+
127
+ 12 住宅 336291 non-null float64