Jupyter Notebookで購買のデータを分析していました。
試行錯誤しながら進めていたのですが、
作業の途中で突然、
今まで問題なく読み込めていたkeyにerrorが起きるようになり、
pd.to_datetimeやpd.mergeなど、
全ての操作にエラーが起きるようになってしまいました。
何もコードはいじっていないので、
自分でも原因が分かりません。。。
皆様もこういった経験はあるのでしょうか?
これは環境の問題ですか???
解決方法が分かる方はいらっしゃいますでしょうか???
例えば、
Python
1df.keys()
Index(['cst_id', 'date_purchase_c', 'date_purchase_n',** 'yymm_purchase',**
'InsDate', 'dhms_InsDate', 'Trn_Type', 'rev', 'num', 'code', 'Brand',
'MdName', 'TranKey', 'TranLineNo', 'Store_code', 'TerminalNo',
'date_purchase', 'TranNo', 'StoreName', 'St_SalesChannel',
'St_Hierarchy', 'Category_c', 'Item_c', 'Sub_Item_c', 'Category',
'Item', 'Sub_Item', 'Sub_Item_2', 'FY'],
dtype='object')
なのに、
Python
1df =pd.to_datetime(df['yymm_purchase'])
###エラーメッセージ
KeyError Traceback (most recent call last)
<ipython-input-56-7d6126cfd80c> in <module>
----> 1 df =pd.to_datetime(df['yymm_purchase'])
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\series.py in getitem(self, key)
866 key = com.apply_if_callable(key, self)
867 try:
--> 868 result = self.index.get_value(self, key)
869
870 if not is_scalar(result):
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_value(self, series, key)
4373 try:
4374 return self._engine.get_value(s, k,
-> 4375 tz=getattr(series.dtype, 'tz', None))
4376 except KeyError as e1:
4377 if len(self) > 0 and (self.holds_integer() or self.is_boolean()):
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/index_class_helper.pxi in pandas._libs.index.Int64Engine._check_type()
KeyError: 'yymm_purchase'
all codes below
Python
1import pandas as pd 2import numpy as np 3import scipy as sp 4import matplotlib.pyplot as plt 5import matplotlib as mpl 6import datetime as dt 7import seaborn as sns 8import io 9from dateutil.parser import parse 10%precision 3 11 12data = pd.read_csv(rf'G:\共有.csv', engine='python', 13 encoding='cp932', dtype='object') 14 15df = pd.read_csv(rf'G:\購買履歴', engine='python', 16 encoding='cp932', dtype='object') 17 18#取り込んだデータの確認 19data.info() 20df.keys() 21df.head() 22data.drop_duplicates() #重複の削除 23 24#chnage values into strings 25data['cst'].astype(str) 26data['Store'].astype(str) 27data['Post'].astype(str) 28 29data = data.drop(['date_n', 'birthday_n'], axis = 1) 30 31#カラムをdatetime型に変換 32df =pd.to_datetime(df['date_c']) 33s = df.loc(['yymm_purchase']) 34df = pd.to_datetime(s)
回答3件
あなたの回答
tips
プレビュー