Python uses Pandas to read and write data, including CSV, Excel, SQL, JSON, etc
Environmental preparation:
Before using Pandas, it is necessary to first install Pandas and related dependencies. You can install it using the following command:
python
pip install pandas
In addition, other dependent class libraries need to be installed, such as xlrd (for reading Excel files), openpyxl (for writing Excel files), pyodbc (for connecting to SQL Server databases), psycopg2 (for connecting to PostgreSQL databases), etc. You can install these class libraries through the corresponding commands.
Dataset introduction:
Below is an example dataset, which is a CSV file that contains some basic information about students. The dataset contains fields such as name, age, gender, subject, and score. The website for downloading the dataset is: https://example.com/example.csv
The sample code is as follows:
python
import pandas as pd
#Read CSV file
df = pd.read_csv('example.csv')
#View the first 5 rows of the dataset
print(df.head())
#Writing a dataset to an Excel file
df.to_excel('example.xlsx', index=False)
#Read Excel file
df_excel = pd.read_excel('example.xlsx')
#Writing a dataset to a SQL Server database
import pyodbc
#Connect to database
conn = pyodbc.connect('DRIVER={SQL Server};SERVER=localhost;DATABASE=mydb;UID=username;PWD=password')
#Create Cursor
cursor = conn.cursor()
#Create Table
cursor.execute('CREATE TABLE students (name VARCHAR(255), age INT, gender VARCHAR(255), subject VARCHAR(255), score FLOAT)')
#Insert data into a table
for index, row in df.iterrows():
cursor.execute('INSERT INTO students (name, age, gender, subject, score) VALUES (?, ?, ?, ?, ?)', row['name'], row['age'], row['gender'], row['subject'], row['score'])
#Commit transaction
conn.commit()
#Close database connection
conn.close()
#Read SQL Server data
conn = pyodbc.connect('DRIVER={SQL Server};SERVER=localhost;DATABASE=mydb;UID=username;PWD=password')
df_sql = pd.read_sql('SELECT * FROM students', conn)
#Writing a dataset to a JSON file
df.to_json('example.json', orient='records')
#Reading JSON files
df_json = pd.read_json('example.json')
The above is an example code for using Pandas to read and write data. The code demonstrates how to read CSV files, write Excel files, write SQL Server databases, read SQL Server data, write JSON files, and read JSON files. Please pay attention to modifying the database connection information and file path according to the actual situation.