diff --git a/datascience/tables.py b/datascience/tables.py index 3ed3dcf9e..c346a424d 100644 --- a/datascience/tables.py +++ b/datascience/tables.py @@ -1314,6 +1314,35 @@ def to_df(self): """Convert the table to a Pandas DataFrame.""" return pandas.DataFrame(self._columns) + def to_csv(self, filename): + """Creates a CSV file with the provided filename. + + The CSV is created in such a way that if we run + ``table.to_csv('my_table.csv')`` we can recreate the same table with + ``Table.read_table('my_table.csv')``. + + Args: + ``filename`` (str): The filename of the output CSV file. + + Returns: + None, outputs a file with name ``filename``. + + >>> job = ['a', 'b', 'c', 'd'] + >>> wage = [10, 20, 15, 8] + >>> some_table = Table([job, wage], ['job', 'wage']) + >>> some_table + job | wage + a | 10 + b | 20 + c | 15 + d | 8 + >>> some_table.to_csv('my_table.csv') # doctest: +SKIP + + """ + # We use index = False to avoid the row number output that pandas does + # by default. + self.to_df().to_csv(filename, index = False) + def to_array(self): """Convert the table to a NumPy array.""" dt = np.dtype(list(zip(self.column_labels, diff --git a/docs/tables.rst b/docs/tables.rst index a0579bd5a..456d21f57 100644 --- a/docs/tables.rst +++ b/docs/tables.rst @@ -112,6 +112,7 @@ Exporting / Displaying Table.index_by Table.to_array Table.to_df + Table.to_csv Visualizations