.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_examples/pandas/format06_stacked_oucru_v1.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_format06_stacked_oucru_v1.py: 06. Clean OUCRU v1.0 ===================== .. GENERATED FROM PYTHON SOURCE LINES 6-68 .. code-block:: default :lineno-start: 6 # Libraries import pandas as pd # ----------------------------- # Load data # ----------------------------- # Load data data = pd.read_csv('./data/stacked-oucru/combined_stacked.csv', parse_dates=['date']) # Show print(data) print(data.columns) # ----------------------------- # Format # ----------------------------- # Drop drop = [ 'Unnamed: 0', 'Unnamed: 0.1', 'result_old', 'date_old', 'dsource' ] # Rename rename = { 'study_no' : 'patient', 'date': 'date_collected', 'column': 'code', } # Replace replace = { 'result': { 'True': 1, 'False': 0 } } # Format data = data.drop(columns=drop) data = data.rename(columns=rename) data['id'] = data.index.values data['uuid'] = data.index.values data['date_outcome'] = data.date_collected data = data.replace(replace) data = data[sorted(data.columns.values)] # Keep only those whose result can be cast to number data.result = pd.to_numeric(data.result, errors='coerce') # Remove nan data = data[data.result.notna()] # Show types print(data.dtypes) # Save data.head(10000).to_csv('./outputs/combined_stack_head10000.csv') data.to_csv('./outputs/combined_stacked.csv') .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.000 seconds) .. _sphx_glr_download__examples_pandas_format06_stacked_oucru_v1.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: format06_stacked_oucru_v1.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: format06_stacked_oucru_v1.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_