前提・実現したいこと
購買と顧客データを使ってJupyter Notebook上でデータ加工をしています。
現在は、購買と顧客データを統合したデータから、
IDごとに最初の購入チャネルがアウトレット店だった行を
抽出したいと考えています。
そこで自分で考えたコードを書いてみましたが、
エラーが起きるし、
そもそもやりたいことと違うことをやっている気がしてなりません。
発生している問題・エラーメッセージ
KeyError Traceback (most recent call last) c:\users\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance) 2889 try: -> 2890 return self._engine.get_loc(key) 2891 except KeyError: pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc() pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc() 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: 'date_entry_c' During handling of the above exception, another exception occurred: KeyError Traceback (most recent call last) <ipython-input-35-15374d0f31f5> in <module> 1 #最初の購入がアウトレットの客を抽出 ----> 2 ppl = whole['date_entry_c'][0].query["St_SalesChannel == 'アウトレット店'"] c:\users\lib\site-packages\pandas\core\frame.py in __getitem__(self, key) 2973 if self.columns.nlevels > 1: 2974 return self._getitem_multilevel(key) -> 2975 indexer = self.columns.get_loc(key) 2976 if is_integer(indexer): 2977 indexer = [indexer] c:\users\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance) 2890 return self._engine.get_loc(key) 2891 except KeyError: -> 2892 return self._engine.get_loc(self._maybe_cast_indexer(key)) 2893 indexer = self.get_indexer([key], method=method, tolerance=tolerance) 2894 if indexer.ndim > 1 or indexer.size > 1: pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc() pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc() 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: 'date_entry_c'
該当のソースコード
whole.info() *一部省略
<class 'pandas.core.frame.DataFrame'>
Int64Index: 959005 entries, 0 to 959004
Data columns (total 76 columns):
cst_id 959005 non-null object
date_entry_c_x 959005 non-null object
yymm_entry_x 959005 non-null object
Store_code_x 959005 non-null int64
PostNo_x 959005 non-null object
Addr1_x 959005 non-null object
Main_StoreName_x 959005 non-null object
Main_St_SalesChannel_x 958997 non-null object
Main_St_Region_x 959005 non-null object
Main_St_Hierarchy_x 959005 non-null object
date_entry_dt_x 959005 non-null datetime64[ns]
date_entry_c_y 959005 non-null object
yymm_entry_y 959005 non-null object
Store_code_y 959005 non-null int64
PostNo_y 959005 non-null object
Addr1_y 959005 non-null object
Main_StoreName_y 959005 non-null object
Main_St_SalesChannel_y 958997 non-null object
Main_St_Region_y 959005 non-null object
Main_St_Hierarchy_y 959005 non-null object
date_entry_dt_y 959005 non-null datetime64[ns]
date_purchase_c 959005 non-null datetime64[ns]
yymm_purchase 959005 non-null int64
InsDate 959005 non-null object
Trn_Type 959005 non-null object
rev 959005 non-null int64
num 959005 non-null int64
code 959005 non-null int64
Brand 959005 non-null object
MdName 959005 non-null object
TranKey 959005 non-null int64
TranLineNo 959005 non-null object
Store_code 959005 non-null object
TerminalNo 959005 non-null int64
TranNo 959005 non-null int64
StoreName 959005 non-null object
St_SalesChannel 959005 non-null object
dtypes: datetime64ns, int64(13), object(60)
memory usage: 563.4+ MB
Python
1#最初の購入がアウトレットの客を抽出 2ppl = whole['date_entry_c'][0].query["St_SalesChannel == 'アウトレット店'"]
###試したこと
ppl = whole.groupby('cst_id')['St_SalesChannel'].query("St_SalesChannel == 'アウトレット店'")
これはAttributeError: 'SeriesGroupBy' object has no attribute 'query'
ppl = whole.groupby('cst_id')['St_SalesChannel']
ppl = ppl("St_SalesChannel == 'アウトレット店'")
どうやらGroupbyはどうしても使えないっぽい
質問で分からない部分があれば
編集依頼お願い致します。
何卒宜しくお願い致します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。