Pandas Read Data Files: CSV, Excel, JSON, SQL
To read data files using pandas, you first need to install the pandas library. You can install it using the following command in the command line:
pip install pandas
After installation is complete, you can use the following code to read the data file into a Pandas data frame:
import pandas as pd
# 读取CSV文件
data = pd.read_csv('文件路径')
# 读取Excel文件
data = pd.read_excel('文件路径')
# 读取JSON文件
data = pd.read_json('文件路径')
# 读取SQL文件
import sqlite3
cnx = sqlite3.connect('数据库路径')
data = pd.read_sql_query('SELECT * FROM 表名', cnx)
The file path is the location of the file to be read, which can be a local file system path or a network path. The table name is the name of the SQL table to be read.
After the reading is finished, the data file will be read into a pandas dataframe, which can be used for data analysis and processing.