<jupyter_start><jupyter_text>範例目標:
1. 讀寫csv
2. 讀寫excel
3. 讀寫json
4. 讀寫SQL資料庫
 範例重點:
1. 讀寫不同檔案格式會有不同程式可以達到csv(read_csv、to_csv)、excel(read_excel、to_excel)、json(read_json、to_json)、SQL資料庫(io.sql.read_sql、to_sql)
2. 讀取進來後都是DataFrame的型態<jupyter_code>import pandas as pd
!pip install openpyxl
!pip install XLRD
iris_data = pd.read_csv('iris.csv')
iris_data
iris_data = pd.read_csv('iris.csv',usecols=['petal length','petal width','target'])
iris_data
iris_data = pd.read_csv('iris.csv',header=0,names=['featrue1','featrue2','featrue3','featrue4','target'])
iris_data
iris_data.to_csv('my_iris.csv')
data = pd.read_excel('data.xls')
data
boston_data = pd.read_excel('data.xls',sheet_name='boston')
boston_data
boston_data = pd.read_excel('data.xls',sheet_name='boston',header=0 \
                            ,usecols=['TAX','PTRATIO','B','LSTAT'])
boston_data
boston_data.to_excel('my_boston.xlsx',sheet_name='boston')
boston_data[:5]
import json
boston_data = pd.read_json('boston.json')
boston_data
boston_data.to_json('boston.json')
import sqlite3
boston_data = pd.read_excel('data.xls',sheet_name='boston',header=0 \
                            ,usecols=['TAX','PTRATIO','B','LSTAT'])
connection = sqlite3.connect('sql_db.sqlite')
boston_data.to_sql('boston', connection, if_exists='replace')
connection.commit()
connection.close()
connection = sqlite3.connect('sql_db.sqlite')
boston_data_sql = pd.io.sql.read_sql("select * from boston",connection)
connection.close()
boston_data_sql
boston_data_sql<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup