実現したいこと
dtype=objectのカラムを使ったpivot_tableで、合計を表示したい。
前提
画像のようなCSVを利用し
index:「都道府県」、columns:「種別」、aggfunc:'size'
でpivot_tableを作成。
その際、margines=Trueにするとエラーが発生します。
発生している問題・エラーメッセージ
KeyError Traceback (most recent call last)
/usr/local/lib/python3.8/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
3360 try:
-> 3361 return self._engine.get_loc(casted_key)
3362 except KeyError as err:
10 frames
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: '個人'
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
/usr/local/lib/python3.8/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
3361 return self._engine.get_loc(casted_key)
3362 except KeyError as err:
-> 3363 raise KeyError(key) from err
3364
3365 if is_scalar(key) and isna(key) and not self.hasnans:
KeyError: '個人'
該当のソースコード
python
1import pandas as pd 2sample_csv = pd.read_csv('/content/sample.csv') 3sample_csv.pivot_table(index='都道府県', columns='種別' ,aggfunc='size', fill_value=0, margins=True)
試したこと
size以外の集計方法(sum, count)を試しましたが、希望の数値になりませんでした。。。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。

回答1件
あなたの回答
tips
プレビュー