前提・実現したいこと
以下の図のように、RDBMSなどからとってきた2つのテーブルを、あるカラムをキーにして左側結合し、紐づかない行のみを判別するために色を塗るか、判別列を追加するような処理を書きたいと思っています。
データ取得してpandasのdfに取り込み、紐づかない行を抽出することはできたのですが、
これらに対し、df.styleで色を付けたりなどするとき、applyの使い方などが悪いのか、
以下のエラーが出ます。エラーについて検索したのですが、何が問題かがよくわかりませんでした。
なお、コードはVSCodeでipynbに記載し、逐次実行しています。何か良い解決方法などありましたらご教示いただけますと幸いです。
(コードが汚いかもしれませんがご容赦いただきたく…。)
発生している問題・エラーメッセージ
エラーメッセージ Int64Index([0], dtype='int64') --------------------------------------------------------------------------- TypeError Traceback (most recent call last) C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\formatters.py in __call__(self, obj) 343 method = get_real_method(obj, self.print_method) 344 if method is not None: --> 345 return method() 346 return None 347 else: C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\formats\style.py in _repr_html_(self) 162 Hooks into Jupyter notebook rich display system. 163 """ --> 164 return self.render() 165 166 @Appender( C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\formats\style.py in render(self, **kwargs) 516 * table_attributes 517 """ --> 518 self._compute() 519 # TODO: namespace all the pandas keys 520 d = self._translate() C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\formats\style.py in _compute(self) 589 r = self 590 for func, args, kwargs in self._todo: --> 591 r = func(self)(*args, **kwargs) 592 return r 593 C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\formats\style.py in _apply(self, func, axis, subset, **kwargs) 597 data = self.data.loc[subset] 598 if axis is not None: --> 599 result = data.apply(func, axis=axis, result_type="expand", **kwargs) 600 result.columns = data.columns 601 else: C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py in apply(self, func, axis, broadcast, raw, reduce, result_type, args, **kwds) 6911 kwds=kwds, 6912 ) -> 6913 return op.get_result() 6914 6915 def applymap(self, func): C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\apply.py in get_result(self) 184 return self.apply_raw() 185 --> 186 return self.apply_standard() 187 188 def apply_empty_result(self): C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\apply.py in apply_standard(self) 290 291 # compute the result using the series generator --> 292 self.apply_series_generator() 293 294 # wrap results C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\apply.py in apply_series_generator(self) 319 try: 320 for i, v in enumerate(series_gen): --> 321 results[i] = self.f(v) 322 keys.append(v.name) 323 except Exception as e: <ipython-input-26-b2380a9baf71> in index_match(s) 1 def index_match(s): 2 print(result_df.index) ----> 3 is_match = s.index in result_df.index 4 return ['background-color:red' if v else '' for v in is_match] C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\numeric.py in __contains__(self, key) 209 Check if key is a float and has a decimal. If it has, return False. 210 """ --> 211 hash(key) 212 try: 213 if is_float(key) and int(key) != key: C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in __hash__(self) 4255 4256 def __hash__(self): -> 4257 raise TypeError("unhashable type: %r" % type(self).__name__) 4258 4259 def __setitem__(self, key, value): TypeError: ("unhashable type: 'RangeIndex'", 'occurred at index aa')
該当のソースコード
Python3
1ソースコード 2def index_match(s): 3 print(result_df.index) 4 is_match = s.index in result_df.index 5 return ['background-color:red' if v else '' for v in is_match] 6 7 8df_l = pd.DataFrame([["1","2",3],[4,5,6],[7,8,9]], columns=["aa","bb", "cc"]) 9df_r = pd.DataFrame([["1","2","3"],[4,5,6],[7,8,9]], columns=["dd","bb","cc"]) 10result_df = pd.merge(df_l, df_r, on=["bb", "cc"], how="left").query("dd != dd") 11 12df_l.style.apply(index_match) 13
試したこと
applyの使い方などについて検索し、試行錯誤してみた。
補足情報(FW/ツールのバージョンなど)
Python 3.7.4
pandas 0.25.1
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/05/20 07:41
2021/05/20 12:30
2021/05/20 23:51
2021/05/21 03:42