質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.41%
Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

0回答

468閲覧

Python データを用いて記述統計を計算したい

daisuke0925

総合スコア5

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2023/01/23 22:06

編集2023/01/24 14:34

Pythonで、スポーツの実施状況等に関する世論調査(令和3年11月調査)のローデータをすべて用いて、男女別の身長と体重の記述統計(平均値、中央値、標準偏差、四分位範囲)を計算したいのですが、

csvファイルにデータを読み込んだ後に、

print(df.describe())

SAMPLENUMBER Q1 Q2.1 Q3 Q4 Q5.1 Q5.2 Q6 Q7 Q7-18
count 2000 2001 2001 2001 2001 2001 2001 2001 2001 40
unique 2000 3 63 48 5 52 72 5 20 28
top 18001 2 70 40 2 160 50 4 15 パート
freq 1 1406 73 212 688 155 142 1928 497 8

... Q48-4 Q48-5 Q48-6 Q48-7 Q48-8 Q48-9 Q48-10 Q48-11 Q48-11-F Q48-12

count ... 2001 2001 2001 2001 2001 2001 2001 2001 8 2001
unique ... 3 3 3 3 3 3 3 3 8 3
top ... 0 0 0 0 0 0 0 0 その他:_ 1
freq ... 1881 1956 1903 1861 1847 1948 1828 1993 1 1270

[4 rows x 2914 columns]

df.iloc[1:].mean()[['Q5.1','Q1']]
と入力してみると、

C:\Users\Owner\AppData\Local\Temp\ipykernel_2652\3443647166.py:1: FutureWarning: Dropping of nuisance columns in DataFrame reductions (with 'numeric_only=None') is deprecated; in a future version this will raise TypeError. Select only valid columns before calling the reduction.
df.iloc[1:].mean()[['Q5.1','Q1']]

KeyError Traceback (most recent call last)
Input In [12], in <cell line: 1>()
----> 1 df.iloc[1:].mean()[['Q5.1','Q1']]

File ~\anaconda3\lib\site-packages\pandas\core\series.py:984, in Series.getitem(self, key)
981 key = np.asarray(key, dtype=bool)
982 return self._get_values(key)
--> 984 return self._get_with(key)

File ~\anaconda3\lib\site-packages\pandas\core\series.py:1024, in Series._get_with(self, key)
1021 return self.iloc[key]
1023 # handle the dup indexing case GH#4246
-> 1024 return self.loc[key]

File ~\anaconda3\lib\site-packages\pandas\core\indexing.py:967, in _LocationIndexer.getitem(self, key)
964 axis = self.axis or 0
966 maybe_callable = com.apply_if_callable(key, self.obj)
--> 967 return self._getitem_axis(maybe_callable, axis=axis)

File ~\anaconda3\lib\site-packages\pandas\core\indexing.py:1191, in _LocIndexer._getitem_axis(self, key, axis)
1188 if hasattr(key, "ndim") and key.ndim > 1:
1189 raise ValueError("Cannot index with multidimensional key")
-> 1191 return self._getitem_iterable(key, axis=axis)
1193 # nested tuple slicing
1194 if is_nested_tuple(key, labels):

File ~\anaconda3\lib\site-packages\pandas\core\indexing.py:1132, in _LocIndexer._getitem_iterable(self, key, axis)
1129 self._validate_key(key, axis)
1131 # A collection of keys
-> 1132 keyarr, indexer = self._get_listlike_indexer(key, axis)
1133 return self.obj._reindex_with_indexers(
1134 {axis: [keyarr, indexer]}, copy=True, allow_dups=True
1135 )

File ~\anaconda3\lib\site-packages\pandas\core\indexing.py:1327, in _LocIndexer._get_listlike_indexer(self, key, axis)
1324 ax = self.obj._get_axis(axis)
1325 axis_name = self.obj._get_axis_name(axis)
-> 1327 keyarr, indexer = ax._get_indexer_strict(key, axis_name)
1329 return keyarr, indexer

File ~\anaconda3\lib\site-packages\pandas\core\indexes\base.py:5782, in Index._get_indexer_strict(self, key, axis_name)
5779 else:
5780 keyarr, indexer, new_indexer = self._reindex_non_unique(keyarr)
-> 5782 self._raise_if_missing(keyarr, indexer, axis_name)
5784 keyarr = self.take(indexer)
5785 if isinstance(key, Index):
5786 # GH 42790 - Preserve name from an Index

File ~\anaconda3\lib\site-packages\pandas\core\indexes\base.py:5842, in Index._raise_if_missing(self, key, indexer, axis_name)
5840 if use_interval_msg:
5841 key = list(key)
-> 5842 raise KeyError(f"None of [{key}] are in the [{axis_name}]")
5844 not_found = list(ensure_index(key)[missing_mask.nonzero()[0]].unique())
5845 raise KeyError(f"{not_found} not in index")

KeyError: "None of [Index(['Q5.1', 'Q1'], dtype='object')] are in the [index]"

と表示されてしまうのですが、どう対応すればよいのかを教えていただきたいです。
(データのQ1は性別で、1は男性、2は女性、Q5.1は身長です)

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

melian

2023/01/24 00:53

以下のページで公開されているデータをそのままで使用しているのだとすれば、 令和3年度「スポーツの実施状況等に関する世論調査」(令和3年11月調査):スポーツ庁 https://www.mext.go.jp/sports/b_menu/toukei/chousa04/sports/1415963_00006.htm 以下の様にするとよいかもしれません。(ワーニングメッセージが表示される場合があります) df.iloc[1:].mean()[['Q5.1','Q1']]
daisuke0925

2023/01/24 10:29

ありがとうございます。 指定されたように入力してみると、 df.iloc[1:].mean()[['Q5.1','Q1']] C:\Users\Owner\AppData\Local\Temp\ipykernel_2652\3443647166.py:1: FutureWarning: Dropping of nuisance columns in DataFrame reductions (with 'numeric_only=None') is deprecated; in a future version this will raise TypeError. Select only valid columns before calling the reduction. df.iloc[1:].mean()[['Q5.1','Q1']] KeyError Traceback (most recent call last) Input In [12], in <cell line: 1>() ----> 1 df.iloc[1:].mean()[['Q5.1','Q1']] File ~\anaconda3\lib\site-packages\pandas\core\series.py:984, in Series.__getitem__(self, key) 981 key = np.asarray(key, dtype=bool) 982 return self._get_values(key) --> 984 return self._get_with(key) File ~\anaconda3\lib\site-packages\pandas\core\series.py:1024, in Series._get_with(self, key) 1021 return self.iloc[key] 1023 # handle the dup indexing case GH#4246 -> 1024 return self.loc[key] File ~\anaconda3\lib\site-packages\pandas\core\indexing.py:967, in _LocationIndexer.__getitem__(self, key) 964 axis = self.axis or 0 966 maybe_callable = com.apply_if_callable(key, self.obj) --> 967 return self._getitem_axis(maybe_callable, axis=axis) File ~\anaconda3\lib\site-packages\pandas\core\indexing.py:1191, in _LocIndexer._getitem_axis(self, key, axis) 1188 if hasattr(key, "ndim") and key.ndim > 1: 1189 raise ValueError("Cannot index with multidimensional key") -> 1191 return self._getitem_iterable(key, axis=axis) 1193 # nested tuple slicing 1194 if is_nested_tuple(key, labels): File ~\anaconda3\lib\site-packages\pandas\core\indexing.py:1132, in _LocIndexer._getitem_iterable(self, key, axis) 1129 self._validate_key(key, axis) 1131 # A collection of keys -> 1132 keyarr, indexer = self._get_listlike_indexer(key, axis) 1133 return self.obj._reindex_with_indexers( 1134 {axis: [keyarr, indexer]}, copy=True, allow_dups=True 1135 ) File ~\anaconda3\lib\site-packages\pandas\core\indexing.py:1327, in _LocIndexer._get_listlike_indexer(self, key, axis) 1324 ax = self.obj._get_axis(axis) 1325 axis_name = self.obj._get_axis_name(axis) -> 1327 keyarr, indexer = ax._get_indexer_strict(key, axis_name) 1329 return keyarr, indexer File ~\anaconda3\lib\site-packages\pandas\core\indexes\base.py:5782, in Index._get_indexer_strict(self, key, axis_name) 5779 else: 5780 keyarr, indexer, new_indexer = self._reindex_non_unique(keyarr) -> 5782 self._raise_if_missing(keyarr, indexer, axis_name) 5784 keyarr = self.take(indexer) 5785 if isinstance(key, Index): 5786 # GH 42790 - Preserve name from an Index File ~\anaconda3\lib\site-packages\pandas\core\indexes\base.py:5842, in Index._raise_if_missing(self, key, indexer, axis_name) 5840 if use_interval_msg: 5841 key = list(key) -> 5842 raise KeyError(f"None of [{key}] are in the [{axis_name}]") 5844 not_found = list(ensure_index(key)[missing_mask.nonzero()[0]].unique()) 5845 raise KeyError(f"{not_found} not in index") KeyError: "None of [Index(['Q5.1', 'Q1'], dtype='object')] are in the [index]" と表示されてしまいました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.41%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問