Pandas 文件读取与存储:CSV、HDF、JSON、Excel 和 SQL

作者:rousong2024.01.22 07:12浏览量:69

简介:在数据科学和机器学习中,Pandas 是一个强大的数据处理库。本文将介绍如何使用 Pandas 读取和存储不同格式的文件,包括 CSV、HDF、JSON、Excel 和 SQL 数据库。

千帆应用开发平台“智能体Pro”全新上线 限时免费体验

面向慢思考场景,支持低代码配置的方式创建“智能体Pro”应用

立即体验

Pandas 是 Python 中用于数据处理和分析的强大库。它提供了许多功能,包括数据读取、清洗、处理和存储。在数据科学和机器学习中,我们经常需要从各种格式的文件中读取数据,并将其存储在适当的格式中。下面我们将介绍如何使用 Pandas 读取和存储 CSV、HDF、JSON、Excel 和 SQL 文件。
1. CSV 文件读取与存储
CSV 是一种常见的数据格式,可以轻松地在各种应用程序之间传输和共享。
读取 CSV 文件:

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

将 DataFrame 写入 CSV 文件:

  1. data.to_csv('output.csv', index=False)

2. HDF 文件读取与存储
HDF(Hierarchical Data Format)是一种用于存储大量数据的文件格式。
读取 HDF 文件:

  1. import pandas as pd
  2. data = pd.read_hdf('data.h5')

将 DataFrame 写入 HDF 文件:

  1. data.to_hdf('output.h5', key='df')

3. JSON 文件读取与存储
JSON(JavaScript Object Notation)是一种轻量级的数据交换格式。
读取 JSON 文件:

  1. import pandas as pd
  2. data = pd.read_json('data.json')

将 DataFrame 写入 JSON 文件:

  1. data.to_json('output.json', orient='records')

4. Excel 文件读取与存储
Excel 是另一种常见的电子表格格式,广泛用于数据分析和可视化。
读取 Excel 文件:
首先,需要安装 openpyxlxlrd 库来读取 Excel 文件。然后,可以使用以下代码:

  1. import pandas as pd
  2. data = pd.read_excel('data.xlsx')

将 DataFrame 写入 Excel 文件:
首先,需要安装 openpyxlxlsxwriter 库来写入 Excel 文件。然后,可以使用以下代码:

  1. data.to_excel('output.xlsx', index=False)

5. SQL 数据库读取与存储
SQL(Structured Query Language)是用于管理关系数据库的标准语言。Pandas 提供了一些方便的函数来连接和查询 SQL 数据库。
连接到 SQL 数据库:
首先,需要安装 sqlalchemy 库来连接 SQL 数据库。然后,可以使用以下代码:
```python
from sqlalchemy import createengine, MetaData, Table, select, and
import pandas as pd
import sqlite3 as sql3 # For SQLite3 connection and operations only if you are using SQLite3 as your database engine. Else you can import respective module for your database engine like MySQLdb for MySQL or pyodbc for SQL Server etc. here. However for the sake of this example we will assume SQLite3 database. Install the respective module by pip install moduleName (like pip install mysqlclient or pip install pyodbc) if you are not using SQLite3 and replace sql3 import statement with respective import statement. # For SQLite3 connection and operations only if you are using SQLite3 as your database engine. Else you can import respective module for your database engine like MySQLdb for MySQL or pyodbc for SQL Server etc. here. However for the sake of this example we will assume SQLite3 database. Install the respective module by pip install moduleName (like pip install mysqlclient or pip install pyodbc) if you are not using SQLite3 and replace sql3 import statement with respective import statement. # For SQLite3 connection and operations only if you are using SQLite3 as your database engine. Else you can import respective module for your database engine like MySQLdb for MySQL or pyodbc for SQL Server etc. here. However for the sake of this example we will assume SQLite3 database. Install the respective module by pip install moduleName (like pip install mysqlclient or pip install pyodbc) if you are not using SQLite3 and replace sql3 import statement with respective import statement

article bottom image

相关文章推荐

发表评论

图片