Last active
May 25, 2020 07:57
-
-
Save polyrand/58bab75b4b790a4425418d80dcd6d558 to your computer and use it in GitHub Desktop.
TFG snippet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| new_cols_nodrug = [c for c in df.columns.to_list() if c not in all_drug_cols] | |
| new_cols = new_cols_nodrug.copy() | |
| for i in range(len(drugs)): | |
| new_cols.append(f"drug_{i}") | |
| new_cols.append(f"pasi_inicial_{i}") | |
| new_cols.append(f"tiempo_{i}") | |
| new_cols.append(f"motivo_{i}") | |
| new_df = pd.DataFrame(columns=new_cols) | |
| final_df = pd.DataFrame(columns=new_cols) | |
| for i, pat in df.iterrows(): | |
| new_df = pd.DataFrame(columns=new_cols) | |
| assert len(new_df.values) == 0 | |
| drugs = [ | |
| "etanercept", | |
| "infliximab", | |
| "efalizumab", | |
| "adalimumab", | |
| "ustekinumab", | |
| "secukinumab", | |
| "ixekizumab", | |
| ] | |
| paciente = new_df.append(pat[new_cols_nodrug], ignore_index=True) | |
| for i in range(len(drugs)): | |
| fechas_inicio = [f"fecha_inicio_{d}" for d in drugs] | |
| pat_dates = pat[fechas_inicio].dropna() | |
| # print(pat_dates, "--", len(pat_dates), "\n\n") | |
| if len(pat_dates) > 0: | |
| current_drug = ( | |
| pat_dates[pat_dates == pat_dates.min()].index[0].split("_")[2] | |
| ) | |
| pasi_inic = ( | |
| pat[f"pasi_inicial_{current_drug}"] | |
| if current_drug != "efalizumab" | |
| else np.nan | |
| ) | |
| fin = pat[f"fecha_fin_{current_drug}"] | |
| inicio = pat[f"fecha_inicio_{current_drug}"] | |
| time = (fin - inicio).days | |
| # print(time) | |
| if time < 0: | |
| fin = pat[f"fecha_fin_{current_drug}"] + pd.DateOffset(years=10) | |
| time = (fin - inicio).days | |
| assert time > 0 | |
| motivo = pat[f"motivo_fin_{current_drug}"] | |
| else: | |
| current_drug = np.nan | |
| pasi_inic = np.nan | |
| time = np.nan | |
| motivo = np.nan | |
| paciente[f"drug_{i}"] = current_drug | |
| paciente[f"pasi_inicial_{i}"] = pasi_inic | |
| paciente[f"tiempo_{i}"] = time | |
| paciente[f"motivo_{i}"] = motivo | |
| # print(current_drug) | |
| try: | |
| drugs.remove(current_drug) | |
| except ValueError: | |
| continue | |
| final_df = final_df.append(paciente) | |
| df["año"] = pd.to_datetime(df["fecha_nacimiento"]).dt.year | |
| df["mes"] = pd.to_datetime(df["fecha_nacimiento"]).dt.month | |
| df["dia"] = pd.to_datetime(df["fecha_nacimiento"]).dt.day | |
| df["sexo"] = df["sexo"].map({"masculino": 1, "femenino": 0}).astype(int) | |
| df["dm"] = df["dm"].map({"si": 1, "no": 0}).astype(int) | |
| df["hta"] = df["hta"].map({"si": 1, "no": 0}).astype(int) | |
| df["dlp"] = df["dlp"].map({"si": 1, "no": 0}).astype(int) | |
| df["otras_comorbilidades"] = ( | |
| df["otras_comorbilidades"].map({"si": 1, "no": 0}).astype(int) | |
| ) | |
| df["tto_previo_ciclosporina"] = ( | |
| df["tto_previo_ciclosporina"].map({"si": 1, "no": 0}).astype(int) | |
| ) | |
| df["tto_previo_mtx"] = df["tto_previo_mtx"].map({"si": 1, "no": 0}).astype(int) | |
| df["tto_previo_acitretino"] = ( | |
| df["tto_previo_acitretino"].map({"si": 1, "no": 0}).astype(int) | |
| ) | |
| df["tto_previo_fototerapia"] = ( | |
| df["tto_previo_fototerapia"].map({"si": 1, "no": 0}).astype(int) | |
| ) | |
| df["u_actualizacion_año"] = pd.to_datetime(df["última_actualización"]).dt.year | |
| df["u_actualizacion_mes"] = pd.to_datetime(df["última_actualización"]).dt.month | |
| df["u_actualizacion_dia"] = pd.to_datetime(df["última_actualización"]).dt.day | |
| df["artritis"] = ( | |
| df["artritis"].map({"confirmada": 2, "posible": 1, "no": 0}).astype(float) | |
| ) | |
| df["derivado_a_otro_centro"] = ( | |
| df["derivado_a_otro_centro"].map({"si": 1, np.nan: 0}).astype(int) | |
| ) | |
| df.drop(labels=["fecha_nacimiento", "última_actualización"], axis=1, inplace=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment