Pandas Input/Output (I/O) 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).
Here are the commonly used Input/Output (I/O) APIs in Pandas:
Read 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 result. |
pd.read_gbq(query) |
Read data from Google BigQuery. |
Write the 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 the DataFrame to a dictionary. |
DataFrame.to_numpy() |
Convert the DataFrame to a numpy array. |
Example
Example
import
pandas
as
pd
Read the CSV file
df
=
pd.
read_csv
(
'data.csv'
)
Read Excel files
df_excel
=
pd.
read_excel
(
'data.xlsx'
,
sheet_name
=
'Sheet1'
)
Read a JSON file
df_json
=
pd.
read_json
(
'data.json'
)
Write the CSV file
df.
to_csv
(
'output.csv'
,
index
=
False
)
Write an Excel file
df.
to_excel
(
'output.xlsx'
,
sheet_name
=
'Sheet1'
)
Write a JSON file
df.
to_json
(
'output.json'
)
Detailed parameter description
pd.read_csv()
| Parameters | Description |
|---|---|
filepath |
File path. |
sep |
Separator, defaults to ,。 |
header |
Row number to use as column names, defaults to 0(the first row). |
index_col |
Column number or name to use as the index. |
dtype |
Specify the data type of a column. |
na_values |
Specify which values should be treated as missing values. |
DataFrame.to_csv()
| Parameters | Description |
|---|---|
path |
File path. |
sep |
Separator, defaults to ,。 |
index |
Whether to write the index, defaults to True。 |
header |
Whether to write the column names, defaults to True。 |
encoding |
File encoding, defaults to utf-8。 |
pd.read_excel()
| Parameters | Description |
|---|---|
io |
File path or file object. |
sheet_name |
Worksheet name or index, defaults to 0。 |
header |
Row number to use as column names, defaults to 0。 |
index_col |
Column number or name to use as the index. |
DataFrame.to_excel()
| Parameters | Description |
|---|---|
path |
File path. |
sheet_name |
Worksheet name, defaults to Sheet1。 |
index |
Whether to write the index, defaults to True。 |
header |
Whether to write the column names, defaults to True。 |
For more detailed information, refer to Pandas official documentation。