《ML with python cookbook》: Loading Data

1.Loading a CSV File

import pandas as pd

path = 'F:/pycharmFile/input/train_data.csv'

dataframe = pd.read_csv(path)
dataframe.head(2)

 

notes:

    1. see how a dataset is structured beforehand and what parameters we need to set to loas in the file.

    2. read_csv has many parameter.

         e.g.  separators; if a header row exist (header=None) ...

 

2.Loading a Excel File

path = 'F:/pycharmFile/input/data.xlsx'
dataframe = pd.read_excel(path, sheet_name=0, header=1)
dataframe.head(2)

the main difference is the additional parameter ---- sheet_name, that specifies which sheet in the Excel file we wish to load.

 

3.Loading a JSON File

# Load library
import pandas as pd
# Create URL
url = 'https://tinyurl.com/simulated_json'
# Load data
dataframe = pd.read_json(url, orient='columns')
# View the first two rows
dataframe.head(2)

 

4.Querying a SQL Database

# Load libraries
import pandas as pd
from sqlalchemy import create_engine
# Create a connection to the database
database_connection = create_engine('sqlite:///sample.db')
# Load data
dataframe = pd.read_sql_query('SELECT * FROM data', database_connection)
# View first two rows
dataframe.head(2)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值