前提・実現したいこと
このような「Table」において、
ra | dec | nDetections |
---|---|---|
123.456 | 5.555 | 3 |
123.456 | 5.555 | 6 |
123.456 | 5.555 | 9 |
456.789 | 6.666 | 10 |
456.789 | 6.666 | 7 |
456.789 | 6.666 | 11 |
このような「Table」において、ra,decが同じものをグループ化し、そのうち
nDetectionsが最大のもののみ抽出し新たに表にする
ra | dec | nDetections |
---|---|---|
123.456 | 5.555 | 9 |
456.789 | 6.666 | 11 |
というコードを作りたいと考えています。
ソースコードは以下のページを参考に書いたものです。
https://qiita.com/ftnext/items/48b7baa49217fdc37ae4
該当のソースコード
python3
1import pandas as pd 2nDetection_max = Table.groupby('ra').max()['nDetections'] 3pd.DataFrame([Table[(Table['ra'] == i) & (Table['nDetections'] == nDetection_max[i])]\ 4 .values[0] for i in range(1, len(nDetection_max)+1)],\ columns=Table.columns)
発生している問題・エラーメッセージ
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) ~/anaconda3/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance) 2524 try: -> 2525 return self._engine.get_loc(key) 2526 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.Float64HashTable.get_item() pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Float64HashTable.get_item() KeyError: 1.0 During handling of the above exception, another exception occurred: KeyError Traceback (most recent call last) <ipython-input-30-e43b20017fb0> in <module>() ----> 1 pd.DataFrame([Table[(Table['ra'] == i) & (Table['nDetections'] == nDetection_max[i])] .values[0] for i in range(1, len(nDetection_max)+1)] , columns=Table.columns) <ipython-input-30-e43b20017fb0> in <listcomp>(.0) ----> 1 pd.DataFrame([Table[(Table['ra'] == i) & (Table['nDetections'] == nDetection_max[i])] .values[0] for i in range(1, len(nDetection_max)+1)] , columns=Table.columns) ~/anaconda3/lib/python3.6/site-packages/pandas/core/series.py in __getitem__(self, key) 621 key = com._apply_if_callable(key, self) 622 try: --> 623 result = self.index.get_value(self, key) 624 625 if not is_scalar(result): ~/anaconda3/lib/python3.6/site-packages/pandas/core/indexes/numeric.py in get_value(self, series, key) 343 344 k = _values_from_object(key) --> 345 loc = self.get_loc(k) 346 new_values = _values_from_object(series)[loc] 347 ~/anaconda3/lib/python3.6/site-packages/pandas/core/indexes/numeric.py in get_loc(self, key, method, tolerance) 402 pass 403 return super(Float64Index, self).get_loc(key, method=method, --> 404 tolerance=tolerance) 405 406 @cache_readonly ~/anaconda3/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance) 2525 return self._engine.get_loc(key) 2526 except KeyError: -> 2527 return self._engine.get_loc(self._maybe_cast_indexer(key)) 2528 2529 indexer = self.get_indexer([key], method=method, tolerance=tolerance) 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.Float64HashTable.get_item() pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Float64HashTable.get_item() KeyError: 1.0
KeyErrorは辞書に存在しない言葉を使うとでるとのことですが、そもそも辞書登録したり引用したりして居ないのでなんで出てくるのかわからないですが、解決方法を調べてもうまくいかないので、どなたかご教示いただけると幸いです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/03/04 04:44
2019/03/04 05:02
2019/03/04 05:48