<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>
{'fraction_non_alphanumeric': 0.15792922673656618, 'fraction_numerical': 0.011140235910878113, 'mean_word_length': 7.122340425531915, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '481870', 'license': 'MIT', 'n_tokens_mistral': 675, 'n_tokens_neox': 678, 'n_words': 74, 'path': 'Sample Code/Day_09_SampleCode.ipynb', 'repo_name': 'YenLinWu/DataScienceMarathon', 'size': '1526'}