.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_write_df.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. or to run this example in your browser via JupyterLite .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_write_df.py: Writing data to RDA and RDS files ================================= An example showing how to write dataframes to RDA and RDS files. .. GENERATED FROM PYTHON SOURCE LINES 8-11 .. code-block:: Python # sphinx_gallery_thumbnail_path = '_static/R_logo.svg' .. GENERATED FROM PYTHON SOURCE LINES 12-13 Let's import Pandas to create dataframe and rdata to write to file. .. GENERATED FROM PYTHON SOURCE LINES 13-18 .. code-block:: Python import pandas as pd import rdata .. GENERATED FROM PYTHON SOURCE LINES 19-20 Let's create a toy dataframe .. GENERATED FROM PYTHON SOURCE LINES 20-27 .. code-block:: Python data = pd.DataFrame({ "class": pd.Categorical(["a", "b", "b"]), "value": [1, 2, 3], }) data .. raw:: html
class value
0 a 1
1 b 2
2 b 3


.. GENERATED FROM PYTHON SOURCE LINES 28-29 Let's write this dataframe to an RDS file. .. GENERATED FROM PYTHON SOURCE LINES 29-32 .. code-block:: Python rdata.write_rds("data.rds", data) .. GENERATED FROM PYTHON SOURCE LINES 33-35 By default, the file is written in a binary XDR format with gzip compression. We can write the file in different formats and compressions too. .. GENERATED FROM PYTHON SOURCE LINES 35-39 .. code-block:: Python rdata.write_rds("data_uncompressed.rds", data, compression=None) rdata.write_rds("data_ascii.rds", data, file_format="ascii", compression=None) .. GENERATED FROM PYTHON SOURCE LINES 40-41 We can also write multiple variables with given names to an RDA file. .. GENERATED FROM PYTHON SOURCE LINES 41-53 .. code-block:: Python df1 = pd.DataFrame({ "class": pd.Categorical(["a", "b", "b"]), "value": [1, 2, 3], }) df2 = pd.DataFrame({ "color": ["red", "green", "blue"], "count": [12, 34, 56], }) data = {"df1": df1, "df2": df2} rdata.write_rda("data.rda", data) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.016 seconds) .. _sphx_glr_download_auto_examples_plot_write_df.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../lite/lab/index.html?path=auto_examples/plot_write_df.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_write_df.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_write_df.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_write_df.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_