pylegs.io.write_table#
- write_table(data: DataFrame | Table, path: str | Path | BytesIO, fmt: str | None = None)[source]#
Write a table to a file in various formats.
- Parameters:
- data
TableLike The table data to write. This can be an
astropy.table.Tableor apandas.DataFrame.- path
PathOrFile The path or file where the table will be written. This can be a string or a
pathlib.Pathobject.- fmt
str, optional The format in which to write the table. If not provided, the format is inferred from the file extension.
- data
- Raises:
ValueErrorIf the provided format is not supported.
Notes
The function supports multiple output formats based on the file extension or the
fmtparameter:'fits','fit': writes to a FITS file.'csv': writes to a CSV file.'parquet': writes to a Parquet file.'dat': writes to a space-separated text file.'tsv': writes to a tab-separated text file.'html': writes to an HTML file.'feather': writes to a Feather file.'vo','vot','votable','xml': writes to a VOTable XML file.
Examples
>>> from astropy.table import Table >>> import pandas as pd >>> data = Table({'col1': [1, 2], 'col2': [3, 4]}) >>> write_table(data, 'output.fits') >>> df = pd.DataFrame({'col1': [1, 2], 'col2': [3, 4]}) >>> write_table(df, 'output.csv')