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]'
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/12 19:34