Pandas provides a variety of functions to read data from different file formats and to write data to those formats. Here’s a comprehensive overview of how to handle data input and output with Pandas.
import pandas as pd
# Reading a CSV file into a DataFrame
df = pd.read_csv('file.csv')
# Reading a CSV file with specific settings
df = pd.read_csv('file.csv', delimiter=',', header=0, names=['A', 'B', 'C'], usecols=['A', 'B'])
# Reading an Excel file into a DataFrame
df = pd.read_excel('file.xlsx', sheet_name='Sheet1')
# Reading multiple sheets
dfs = pd.read_excel('file.xlsx', sheet_name=['Sheet1', 'Sheet2'])
import sqlite3
# Connecting to a SQLite database
conn = sqlite3.connect('database.db')
# Reading a SQL query into a DataFrame
df = pd.read_sql_query('SELECT * FROM table_name', conn)
# Reading a JSON file into a DataFrame
df = pd.read_json('file.json')
# Reading from a JSON string
data = '{"A": [1, 2, 3], "B": [4, 5, 6]}'
df = pd.read_json(data)
# Reading tables from an HTML file
dfs = pd.read_html('file.html')
# Reading from a URL
dfs = pd.read_html('http://example.com/file.html')
# Writing a DataFrame to a CSV file
df.to_csv('file.csv')
# Writing with specific settings
df.to_csv('file.csv', sep=',', index=False, columns=['A', 'B'])
# Writing a DataFrame to an Excel file
df.to_excel('file.xlsx', sheet_name='Sheet1')
# Writing multiple DataFrames to multiple sheets
with pd.ExcelWriter('file.xlsx') as writer:
df1.to_excel(writer, sheet_name='Sheet1')
df2.to_excel(writer, sheet_name='Sheet2')
# Writing a DataFrame to a SQL table
df.to_sql('table_name', conn, if_exists='replace', index=False)
# Writing a DataFrame to a JSON file
df.to_json('file.json')
# Writing to a JSON string
json_data = df.to_json()
# Writing a DataFrame to an HTML file
df.to_html('file.html')