Show Menu

Importing Data in Python I Cheat Sheet by

pandas

Importing Text Files I

open(­fil­e_name, 'r')
open the file
file.r­ead()
read the file
file.c­lo­se()
close the file
file.c­lo­sed()
check if the file is closed
It is a good practice to close the file after reading it when using 'open'

Importing Text Files II

with open(f­ile­_name) as file :
open the file
file.r­ead()
read the file
file.r­ead­line()
read line by line
When using the 'with' statement there is no need to close the file

Importing Flat Files with Numpy I

import numpy as np
import numpy
np.lo­adt­xt(­fil­e_name, delimiter= ' ')
importing the file
skipr­ows=1
argument to skip a specific row
useco­ls=[0, 2]
argument to only show specific columns
`dtype = str'
argument to import the data as string
loadtxt only works with numeric data

Importing Flat Files with Numpy II

import numpy as np
import numpy
np.re­cfr­omc­sv(­file, delimi­ter­=",", names=­True, dtype=­None)
open the file
np.ge­nfr­omt­xt(­file, delimi­ter­=',', names=­True, dtype=­None)
open the file
with the functions re­cfr­­om­c­sv() and ge­nfr­­om­t­xt() we are able to import data with different types

Importing Stata Files

import pandas as pd
importing pandas
df = pd.rea­d_s­tat­a('­dis­are­a.d­ta')
reading the stata file
 

Importing Flat Files With Pandas

import pandas as pd
import pandas
pd.re­ad_­csv­(file)
open csv file
nrows=5
argument for the number of rows to load
heade­r=None
argument for no header
sep='\t'
argument to set delimiter
comme­nt='#'
argument takes characters that comments occur after in the file
na_va­lue­s='­Not­hing'
argument to recognize a string as a NaN Value

Import pickled files

import pickle
import the library
with open(f­ile­_name, 'rb') as file :
open file
pickl­e.l­oad­(file)
read file

Importing Spread­sheet Files

import pandas as pd
importing pandas
pd.Ex­cel­Fil­e(f­ile)
opening the file
xl.sh­eet­_names
exporting the sheet names
xl.pa­rse­(sh­eet­_na­me/­index)
loading a sheet to a dataframe
skipr­ows­=[i­ndex]
skipping a specific row
names­=[List of Names]
naming the sheet's columns
useco­ls=­[0,]
parse spesific columns
skiprows, names and useclos are all arguments of the function parse()

Importing SAS Files

from sas7bdat import SAS7BDAT
importing sas7bdat library
import pandas as pd
importing pandas
with SAS7BD­AT(­'fi­le_­name') as file:
opening the file
file.t­o_­dat­a_f­rame()
loading the file as dataframe
 

Importing HDF5 files

import numpy as np
import numpy
import h5py
importing the h5py library
h5py.F­il­e(file, 'r')
reading the file

Importing MATLAB files

import scipy.io
importing scipy.io
cipy.i­o.l­oa­dma­t('­fil­e_n­ame')
reading the file

Relational databases I

import pandas as pd
importing pandas
from sqlalchemy import create­_en­gine
importing the necessary library
engine = create­_en­gin­e('­dat­aba­set­ype­://­/na­me.d­at­aba­set­ype')
creating an engine
con = engine.co­nne­ct()
connecting to the engine
rs = con.ex­ecu­te(­'SELECT * FROM Album')
performe query
df = pd.Dat­aFr­ame­(rs.fe­tch­all())
save as a dataframe
df.co­lumns = rs.keys
set columns names
con.c­lose()
close the connection
The best practice is to close the connection

Relational databases II

engine = create­_en­gin­e('­dat­aba­set­ype­://­/na­me.d­at­aba­set­ype')
creating an engine
with engine.co­nnect() as con:
connecting to the engine
rs = con.ex­ecu­te('sql code')
performe query
df = pd.Dat­aFr­ame­(rs.fe­tch­man­y(s­ize­=3))
load a number of rows as a dataframe
With 'open' you don't have to close the connection at the end

Relational databases III

engine = create­_en­gin­e('­dat­aba­set­ype­://­/na­me.d­at­aba­set­ype')
creating an engine
df = pd.rea­d_s­ql_­que­ry('SQL code', engine)
performe query
Fastest way to connect to a database and perform query

Download the Importing Data in Python I Cheat Sheet

2 Pages
//media.cheatography.com/storage/thumb/issambd_importing-data-in-python-i.750.jpg

PDF (recommended)

Alternative Downloads

Share This Cheat Sheet!

 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets