Pandas Input/Output API Manual
Pandas is a powerful Python data analysis library that provides a large number of data manipulation tools, including data input and output (I/O).
The following are commonly used Pandas Input/Output APIs:
Reading Data
| Method | Description |
|---|---|
pd.read_csv(filepath, sep, header, index_col) |
Read data from a CSV file. |
pd.read_excel(io, sheet_name) |
Read data from an Excel file. |
pd.read_json(path_or_buf) |
Read data from a JSON file. |
pd.read_html(io) |
Read table data from an HTML file. |
pd.read_sql(sql, con) |
Read data from a SQL database. |
pd.read_clipboard() |
Read data from the clipboard. |
pd.read_parquet(path) |
Read data from a Parquet file. |
pd.read_feather(path) |
Read data from a Feather file. |
pd.read_hdf(path, key) |
Read data from an HDF5 file. |
pd.read_pickle(path) |
Read data from a Pickle file. |
pd.read_sas(filepath) |
Read data from a SAS file. |
pd.read_spss(filepath) |
Read data from an SPSS file. |
pd.read_sql_table(table_name, con) |
Read data from a table in a SQL database. |
pd.read_sql_query(sql, con) |
Execute a SQL query and read the results. |
pd.read_gbq(query) |
Read data from Google BigQuery. |
Writing Data
| Method | Description |
|---|---|
DataFrame.to_csv(path, sep, index) |
Write a DataFrame to a CSV file. |
DataFrame.to_excel(path, sheet_name) |
Write a DataFrame to an Excel file. |
DataFrame.to_json(path) |
Write a DataFrame to a JSON file. |
DataFrame.to_html(path) |
Write a DataFrame to an HTML file. |
DataFrame.to_sql(name, con) |
Write a DataFrame to a SQL database. |
DataFrame.to_clipboard() |
Copy a DataFrame to the clipboard. |
DataFrame.to_parquet(path) |
Write a DataFrame to a Parquet file. |
DataFrame.to_feather(path) |
Write a DataFrame to a Feather file. |
DataFrame.to_hdf(path, key) |
Write a DataFrame to an HDF5 file. |
DataFrame.to_pickle(path) |
Write a DataFrame to a Pickle file. |
DataFrame.to_markdown(path) |
Write a DataFrame to a Markdown file. |
DataFrame.to_string() |
Convert a DataFrame to a string. |
DataFrame.to_latex(path) |
Write a DataFrame to a LaTeX file. |
DataFrame.to_records() |
Convert a DataFrame to a numpy record array. |
DataFrame.to_dict() |
Convert a DataFrame to a dictionary. |
DataFrame.to_numpy() |
Convert a DataFrame to a numpy array. |
Examples
Examples
import pandas as pd
# Read CSV file
df = pd.read_csv('data.csv')
# Read Excel file
df_excel = pd.read_excel('data.xlsx', sheet_name='Sheet1')
# Read JSON file
df_json = pd.read_json('data.json')
# Write CSV file
df.to_csv('output.csv', index=False)
# Write Excel file
df.to_excel('output.xlsx', sheet_name='Sheet1')
# Write JSON file
df.to_json('output.json')
# Read CSV file
df = pd.read_csv('data.csv')
# Read Excel file
df_excel = pd.read_excel('data.xlsx', sheet_name='Sheet1')
# Read JSON file
df_json = pd.read_json('data.json')
# Write CSV file
df.to_csv('output.csv', index=False)
# Write Excel file
df.to_excel('output.xlsx', sheet_name='Sheet1')
# Write JSON file
df.to_json('output.json')
Detailed Parameter Description
pd.read_csv()
| Parameter | Description |
|---|---|
filepath |
File path. |
sep |
Delimiter, default is,。 |
header |
Specify the row number used as column names, default is0(First row). |
index_col |
Specify the column number or column name used as the index. |
dtype |
Specify the data type of the columns. |
na_values |
Specify which values should be treated as missing values. |
DataFrame.to_csv()
| Parameter | Description |
|---|---|
path |
File path. |
sep |
Delimiter, default is,。 |
index |
Whether to write the index, default isTrue。 |
header |
Whether to write column names, default isTrue。 |
encoding |
File encoding, default isutf-8。 |
pd.read_excel()
| Parameter | Description |
|---|---|
io |
File path or file object. |
sheet_name |
Worksheet name or index, default is0。 |
header |
Specify the row number used as column names, default is0。 |
index_col |
Specify the column number or column name used as the index. |
DataFrame.to_excel()
| Parameter | Description |
|---|---|
path |
File path. |
sheet_name |
Worksheet name, default isSheet1。 |
index |
Whether to write the index, default isTrue。 |
header |
Whether to write column names, default isTrue。 |
For more detailed information, please refer toPandas Official Documentation。
Other Extensions