Q&A
1. 実現したいこと
以下のようなデータを扱っています。
df.head(20)
各個人ごとに健康診断時の年齢と計測した西暦、病気を発症した経験がある場合はget_sickが1になっています。
いま、get_sick=0の人が将来病気にかかる可能性を予測するモデルを構築しようとしています。
そのため、get_sick=0の人が1〜5年後(任意)の年数後に0→1に変化しているかを確認し、もし変化していたら新しい列 'history' に1を、0→0であれば0を格納するという処理をしたいと考えています。
すでにget_sick=1となっているデータは学習に使わないのでget_sick=0のみを対象としています。
2. 試したこと
df.groupby('ID').['get_sick'].diff(-1)
を使って差分を取ろうとしたのですが、ageやyearを利用してどのように5年以内という条件を付け加えるのかがわかりませんでした。
Pandasの操作に詳しい方が居られましたら教えてくださると幸いです。
宜しくお願い致します。
3. 出力されたエラー
KeyError: 'str_0_0xb00xa30xf80x170xe80x7f0x00x0' During handling of the above exception, another exception occurred: KeyError Traceback (most recent call last) KeyError: 'str_0_0xb00xa30xf80x170xe80x7f0x00x0' The above exception was the direct cause of the following exception: UndefinedVariableError Traceback (most recent call last) /usr/local/lib/python3.7/dist-packages/pandas/core/computation/scope.py in resolve(self, key, is_local) 214 from pandas.core.computation.ops import UndefinedVariableError 215 --> 216 raise UndefinedVariableError(key, is_local) from err 217 218 def swapkey(self, old_key: str, new_key: str, new_value=None) -> None: UndefinedVariableError: name 'str_0_0xb00xa30xf80x170xe80x7f0x00x0' is not defined
回答1件
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2022/06/14 10:42
2022/06/14 12:47
2022/06/15 00:53