Sunday, 29 January 2023

Data input and Output in Python Pandas

 

Data Input and Output is an essential aspect of working with pandas. The library provides several functions and methods to read and write data to and from different file formats. In this article, we will discuss how to read and write data to and from CSV, Excel, JSON, and SQL file formats using pandas.

CSV

Comma Separated Values (CSV) is one of the most widely used file formats for storing data. pandas provides the read_csv and to_csv functions to read and write CSV files, respectively.

To read a CSV file, use the read_csv() function and pass the file path as an argument. The function returns a DataFrame object.


import pandas as pd
 
df = pd.read_csv('data.csv')

The read_csv() function also accepts several optional parameters to customize the reading process. For example, you can use the header parameter to specify the row number to use as the column names, or use the names parameter to provide a list of column names.

To write a DataFrame to a CSV file, use the to_csv() function and pass the file path as an argument.


df.to_csv('data_modified.csv', index=False)

The to_csv() function also accepts several optional parameters to customize the writing process. For example, you can use the sep parameter to specify the separator character to use between fields, or use the index parameter to specify whether to write the row index to the file.

Excel

Excel is another widely used file format for storing data. pandas provides the read_excel and to_excel functions to read and write Excel files, respectively.

To read an Excel file, use the read_excel() function and pass the file path as an argument. The function returns a DataFrame object.


df = pd.read_excel('data.xlsx')

The read_excel() function also accepts several optional parameters to customize the reading process. For example, you can use the sheet_name parameter to specify the sheet to read from the Excel file, or use the usecols parameter to specify the columns to read.

To write a DataFrame to an Excel file, use the to_excel() function and pass the file path as an argument.


df.to_excel('data_modified.xlsx', index=False)

The to_excel() function also accepts several optional parameters to customize the writing process. For example, you can use the engine parameter to specify the engine to use when writing to the file, or use the columns parameter to specify the columns to write.

JSON

JavaScript Object Notation (JSON) is a lightweight data interchange format. pandas provides the read_json and to_json functions to read and write JSON files, respectively.

To read a JSON file, use the read_json() function and pass the file path as an argument. The function returns a DataFrame object.


df = pd.read_json('data.json')

 

No comments:

Post a Comment