save data table to file in python

To save a data table to a file in Python, you can use the pandas library. Here's an example of how you can save a data table to a CSV file:

main.py
import pandas as pd

# Create a sample data table
data = {'Name': ['John', 'Emma', 'Alex'],
        'Age': [25, 28, 30],
        'City': ['New York', 'London', 'Paris']}

df = pd.DataFrame(data)

# Save the data table to a CSV file
df.to_csv('data_table.csv', index=False)
273 chars
12 lines

In this example, we first create a data table using a Python dictionary. Then we convert the dictionary to a pandas DataFrame. Finally, we use the to_csv() function to save the DataFrame to a CSV file. The index=False argument ensures that the index column is not saved to the file.

You can also save the data table to other file formats, such as Excel, by using the appropriate pandas function (to_excel(), to_json(), etc.) and specifying the desired file extension in the file name.

Remember to have the pandas library installed in your Python environment before running this code.

related categories

gistlibby LogSnag