.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_examples/pandas/plot_format04_therapy_one.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr__examples_pandas_plot_format04_therapy_one.py: 04. Format MIMIC therapy (one) =============================== Description... .. GENERATED FROM PYTHON SOURCE LINES 7-13 .. code-block:: default :lineno-start: 7 # Generic libraries import pandas as pd # Show in terminal TERMINAL = False .. GENERATED FROM PYTHON SOURCE LINES 14-15 First, lets load and do some basic formatting on the data. .. GENERATED FROM PYTHON SOURCE LINES 15-49 .. code-block:: default :lineno-start: 16 # ----------------------------- # Constants # ----------------------------- # Path path = './data/mimic-therapy/One_patient_condensed_10656173.csv' path = './data/mimic-therapy/One_patient_condensed_11803145.csv' # ----------------------------- # Load data # ----------------------------- # Read data data = pd.read_csv(path, dayfirst=True, parse_dates=['starttime', 'stoptime']) # Keep only useful columns data = data[['subject_id', 'antibiotic', 'route', 'starttime', 'stoptime']] # Reformat (ignore time information) data.starttime = data.starttime.dt.date data.stoptime = data.stoptime.dt.date # Show if TERMINAL: print("\nData:") print(data) data .. raw:: html
subject_id antibiotic route starttime stoptime
0 11803145 Vancomycin IV 2159-01-20 2159-01-20
1 11803145 Piperacillin-Tazobactam IV 2159-01-20 2159-01-20
2 11803145 Piperacillin-Tazobactam IV 2159-01-20 2159-01-20
3 11803145 Piperacillin-Tazobactam IV 2159-01-20 2159-01-25
4 11803145 Vancomycin IV 2159-01-21 2159-01-22
5 11803145 Vancomycin IV 2159-01-22 2159-01-23
6 11803145 Vancomycin IV 2159-01-23 2159-01-23
7 11803145 Meropenem IV 2159-01-25 2159-01-26
8 11803145 Meropenem IV 2159-01-25 2159-01-26
9 11803145 Meropenem IV 2159-01-26 2159-02-02
10 11803145 Vancomycin IV 2159-01-26 2159-01-26
11 11803145 Vancomycin IV 2159-01-26 2159-01-27
12 11803145 Vancomycin IV 2159-01-26 2159-01-29
13 11803145 Vancomycin IV 2159-01-28 2159-01-29
14 11803145 Tobramycin Sulfate IV 2159-01-28 2159-01-29
15 11803145 Azithromycin IV 2159-01-28 2159-01-30
16 11803145 MetRONIDAZOLE (FLagyl) IV 2159-01-29 2159-02-01
17 11803145 Vancomycin IV 2159-01-30 2159-01-31
18 11803145 Vancomycin IV 2159-02-01 2159-02-02
19 11803145 Meropenem IV 2159-02-03 2159-02-04
20 11803145 Vancomycin IV 2159-02-03 2159-02-04
21 11803145 Vancomycin IV 2159-02-03 2159-02-04


.. GENERATED FROM PYTHON SOURCE LINES 50-51 Lets transform the data .. GENERATED FROM PYTHON SOURCE LINES 51-77 .. code-block:: default :lineno-start: 52 # ----------------------------- # Transform data # ----------------------------- # .. note: The closed parameter indicates whether to include # the first and/or last samples. None will keep both, # left will keep only start date and right will keep # also the right date. # Create column with date range data['startdate'] = data.apply(lambda x: pd.date_range(start=x['starttime'], end=x['stoptime'], closed='left', # ignoring right freq='D') ,axis=1) # Explode such column data = data.explode('startdate') # Create daily therapies aux = data.groupby('startdate') \ .apply(lambda x: sorted(x.antibiotic \ .str.lower().str.strip().unique().tolist())) # Include missing days aux = aux.asfreq('1D') .. GENERATED FROM PYTHON SOURCE LINES 78-79 Lets see the formatted data .. GENERATED FROM PYTHON SOURCE LINES 79-86 .. code-block:: default :lineno-start: 80 # Show if TERMINAL: print("\nFormatted:") print(aux) aux .. rst-class:: sphx-glr-script-out Out: .. code-block:: none startdate 2159-01-20 [piperacillin-tazobactam, vancomycin] 2159-01-21 [piperacillin-tazobactam, vancomycin] 2159-01-22 [piperacillin-tazobactam, vancomycin] 2159-01-23 [piperacillin-tazobactam, vancomycin] 2159-01-24 [piperacillin-tazobactam] 2159-01-25 [meropenem] 2159-01-26 [meropenem, vancomycin] 2159-01-27 [meropenem, vancomycin] 2159-01-28 [azithromycin, meropenem, tobramycin sulfate, ... 2159-01-29 [azithromycin, meropenem, metronidazole (flagyl)] 2159-01-30 [meropenem, metronidazole (flagyl), vancomycin] 2159-01-31 [meropenem, metronidazole (flagyl)] 2159-02-01 [meropenem, vancomycin] 2159-02-02 NaN 2159-02-03 [meropenem, vancomycin] Freq: D, dtype: object .. GENERATED FROM PYTHON SOURCE LINES 87-88 Lets count the number of days .. GENERATED FROM PYTHON SOURCE LINES 88-94 .. code-block:: default :lineno-start: 89 # Show if TERMINAL: print("\nTherapies (number of days)") print(aux.value_counts()) aux.value_counts() .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [piperacillin-tazobactam, vancomycin] 4 [meropenem, vancomycin] 4 [piperacillin-tazobactam] 1 [meropenem] 1 [azithromycin, meropenem, tobramycin sulfate, vancomycin] 1 [azithromycin, meropenem, metronidazole (flagyl)] 1 [meropenem, metronidazole (flagyl), vancomycin] 1 [meropenem, metronidazole (flagyl)] 1 dtype: int64 .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.039 seconds) .. _sphx_glr_download__examples_pandas_plot_format04_therapy_one.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_format04_therapy_one.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_format04_therapy_one.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_