Import/ Exporting data
Table of contents
Data import and export are fundamental steps in the data analysis process. Pandas, a powerful Python library for data manipulation and analysis, provides versatile tools for reading data from various sources and saving data to different file formats. In this section, we’ll explore how to import and export data using Pandas.
Importing Data (Loading)
One of the most common data formats is Comma-Separated Values (CSV). Pandas makes it straightforward to read data from CSV files into DataFrames:
df = pd.read_csv('data.csv')
Reading Excel Files
Pandas can also read data from Excel files.
df = pd.read_excel('data.xlsx', sheet_name='Sheet1')
Other Data Formats
Pandas supports reading data from various formats, such as JSON, SQL databases, and more. Explore the pd.read_...()
functions for specific formats you need.
Exporting Data (Saving)
Writing to CSV
You can save your DataFrame to a CSV file like this:
df.to_csv('output.csv' )
Set index to False to exclude row numbers index=False
Writing to Excel
Exporting to Excel is also straightforward:
df.to_excel('output.xlsx', sheet_name='Sheet1')
Other Export Formats
Pandas supports exporting to multiple formats, such as JSON, SQL databases, and more. Utilize the df.to_...()
functions to save your data in the desired format.