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

質問編集履歴

3

誤字

2020/07/07 08:10

投稿

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

2

文法の修正

2020/07/07 08:10

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -6,14 +6,24 @@
6
6
  ### 発生している問題・エラーメッセージ
7
7
 
8
8
  ```
9
- /usr/local/lib/python3.6/dist-packages/pandas/core/groupby/grouper.py in get_grouper(obj, key, axis, level, sort, observed, mutated, validate)
10
- 598 in_axis, name, level, gpr = False, None, gpr, None
9
+ KeyError Traceback (most recent call last)
11
- 599 else:
12
- --> 600 raise KeyError(gpr)
10
+ <ipython-input-10-5bf405663c72> in <module>()
11
+ 20
12
+ 21 import plotly_express as px
13
+ ---> 22 df_melt = df.melt(id_vars='date', value_vars=["小売・娯楽", "食料品店・薬局", "公園", "乗換駅", "職場", "住宅"])
14
+ 23 fig = px.line(df_melt, x="date", y="value", color='variable',
13
- 601 elif isinstance(gpr, Grouper) and gpr.key is not None:
15
+ 24 title="Google Mobility Report")
14
- 602 # Add key to exclusions
15
16
 
17
+ 7 frames
18
+ /usr/local/lib/python3.6/dist-packages/pandas/core/indexing.py in _validate_read_indexer(self, key, indexer, axis, raise_missing)
19
+ 1653 if not (ax.is_categorical() or ax.is_interval()):
16
- KeyError: 'JP'
20
+ 1654 raise KeyError(
21
+ -> 1655 "Passing list-likes to .loc or [] with any missing labels "
22
+ 1656 "is no longer supported, see "
23
+ 1657 "https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#deprecate-loc-reindex-listlike" # noqa:E501
24
+
25
+ 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'
26
+
17
27
  ```
18
28
 
19
29
  ### 該当のソースコード
@@ -32,10 +42,13 @@
32
42
  'residential_percent_change_from_baseline': '住宅'})
33
43
 
34
44
  # 日本のデータのみ絞り込む (# データ数 = 6240件)
35
- print(df.info())
36
45
 
37
- df = df.groupby("JP").mean()
46
+ #print(df.info())
38
47
 
48
+ grp = df.groupby('country_region_code').mean()
49
+ df = grp.query('country_region_code == "JP"')
50
+
51
+
39
52
  import plotly_express as px
40
53
  df_melt = df.melt(id_vars='date', value_vars=["小売・娯楽", "食料品店・薬局", "公園", "乗換駅", "職場", "住宅"])
41
54
  fig = px.line(df_melt, x="date", y="value", color='variable',
@@ -43,22 +56,4 @@
43
56
 
44
57
 
45
58
  fig.show()
46
- ```
59
+ ```
47
-
48
- ###実行結果
49
- Data columns (total 13 columns):
50
- Column Non-Null Count Dtype
51
- --- ------ -------------- -----
52
- 0 country_region_code 605422 non-null object
53
- 1 country_region 606467 non-null object
54
- 2 sub_region_1 588497 non-null object
55
- 3 sub_region_2 350398 non-null object
56
- 4 iso_3166_2_code 220137 non-null object
57
- 5 census_fips_code 350532 non-null float64
58
- 6 date 606467 non-null object
59
- 7 小売・娯楽 464961 non-null float64
60
- 8 食料品店・薬局 445097 non-null float64
61
- 9 公園 276593 non-null float64
62
- 10 乗換駅 322793 non-null float64
63
- 11 職場 592165 non-null float64
64
- 12 住宅 336291 non-null float64

1

追記依頼の実行

2020/07/04 23:02

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -32,6 +32,7 @@
32
32
  'residential_percent_change_from_baseline': '住宅'})
33
33
 
34
34
  # 日本のデータのみ絞り込む (# データ数 = 6240件)
35
+ print(df.info())
35
36
 
36
37
  df = df.groupby("JP").mean()
37
38
 
@@ -42,4 +43,22 @@
42
43
 
43
44
 
44
45
  fig.show()
45
- ```
46
+ ```
47
+
48
+ ###実行結果
49
+ Data columns (total 13 columns):
50
+ Column Non-Null Count Dtype
51
+ --- ------ -------------- -----
52
+ 0 country_region_code 605422 non-null object
53
+ 1 country_region 606467 non-null object
54
+ 2 sub_region_1 588497 non-null object
55
+ 3 sub_region_2 350398 non-null object
56
+ 4 iso_3166_2_code 220137 non-null object
57
+ 5 census_fips_code 350532 non-null float64
58
+ 6 date 606467 non-null object
59
+ 7 小売・娯楽 464961 non-null float64
60
+ 8 食料品店・薬局 445097 non-null float64
61
+ 9 公園 276593 non-null float64
62
+ 10 乗換駅 322793 non-null float64
63
+ 11 職場 592165 non-null float64
64
+ 12 住宅 336291 non-null float64