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

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

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

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

pandas

Pandasは、PythonでRにおけるデータフレームに似た型を持たせることができるライブラリです。 行列計算の負担が大幅に軽減されるため、Rで行っていた集計作業をPythonでも比較的簡単に行えます。 データ構造を変更したりデータ分析したりするときにも便利です。

Q&A

解決済

1回答

1134閲覧

Pandasでエラー

_hh

総合スコア79

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

pandas

Pandasは、PythonでRにおけるデータフレームに似た型を持たせることができるライブラリです。 行列計算の負担が大幅に軽減されるため、Rで行っていた集計作業をPythonでも比較的簡単に行えます。 データ構造を変更したりデータ分析したりするときにも便利です。

0グッド

0クリップ

投稿2018/10/12 12:26

編集2018/10/12 12:31

Jupyter notebookで、あるcsvをpandasで処理しています。本来9万行以上あるのですが、なぜかiのrangeの()の中の
値が5より大きくなると、エラーがでます。

理由と対処をご教示ください。

【環境】Windows10 64bit,chrome
【コード】
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
csv_in = 'data.csv'

df = pd.read_csv(csv_in, delimiter=',', encoding='shift-jis')
df = df[['生年月日','階級']]
df = df.replace('7S', '7')
df = df.replace('7F', '7')
df = df.replace('6S', '7')
df = df.replace('6F', '7')
df.dropna(inplace=True)

df2=df
LL = len(df2)

print(LL)
for i in range(6):
print(df.loc[i,['生年月日']])

【エラー】

KeyError Traceback (most recent call last)
~\Miniconda3\envs\stats\lib\site-packages\pandas\core\indexing.py in _validate_key(self, key, axis)
1789 if not ax.contains(key):
-> 1790 error()
1791 except TypeError as e:

~\Miniconda3\envs\stats\lib\site-packages\pandas\core\indexing.py in error()
1784 .format(key=key,
-> 1785 axis=self.obj._get_axis_name(axis)))
1786

KeyError: 'the label [5] is not in the [index]'

During handling of the above exception, another exception occurred:

KeyError Traceback (most recent call last)
<ipython-input-26-eecc260919f8> in <module>()
22 #today = int(pd.to_datetime('today').strftime('%Y%m%d'))
23 for i in range(6):
---> 24 print(df.loc[i,['生年月日']])
25 #birthday= df.loc[i,['生年月日']]
26 # age = int((today-birthday)/10000)

~\Miniconda3\envs\stats\lib\site-packages\pandas\core\indexing.py in getitem(self, key)
1470 except (KeyError, IndexError):
1471 pass
-> 1472 return self._getitem_tuple(key)
1473 else:
1474 # we by definition only have the 0th axis

~\Miniconda3\envs\stats\lib\site-packages\pandas\core\indexing.py in _getitem_tuple(self, tup)
868 def _getitem_tuple(self, tup):
869 try:
--> 870 return self._getitem_lowerdim(tup)
871 except IndexingError:
872 pass

~\Miniconda3\envs\stats\lib\site-packages\pandas\core\indexing.py in _getitem_lowerdim(self, tup)
996 for i, key in enumerate(tup):
997 if is_label_like(key) or isinstance(key, tuple):
--> 998 section = self._getitem_axis(key, axis=i)
999
1000 # we have yielded a scalar ?

~\Miniconda3\envs\stats\lib\site-packages\pandas\core\indexing.py in _getitem_axis(self, key, axis)
1909
1910 # fall thru to straight lookup
-> 1911 self._validate_key(key, axis)
1912 return self._get_label(key, axis=axis)
1913

~\Miniconda3\envs\stats\lib\site-packages\pandas\core\indexing.py in _validate_key(self, key, axis)
1796 raise
1797 except:
-> 1798 error()
1799
1800 def _is_scalar_access(self, key):

~\Miniconda3\envs\stats\lib\site-packages\pandas\core\indexing.py in error()
1783 raise KeyError(u"the label [{key}] is not in the [{axis}]"
1784 .format(key=key,
-> 1785 axis=self.obj._get_axis_name(axis)))
1786
1787 try:

KeyError: 'the label [5] is not in the [index]'

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

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

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

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

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

guest

回答1

0

ベストアンサー

単に、indexが5 の行が dropna() にて削除されたのではないでしょうか。

Index名ではなく絶対座標を使いたい場合は loc[] ではなくiloc[] をお使いください

投稿2018/10/12 12:55

編集2018/10/12 13:08
magichan

総合スコア15898

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

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

_hh

2018/10/12 19:34

解決致しました!有難うございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問