Excel Spreadsheets Pickled Files
Python For Data Science Cheat Sheet
>>> file = ‘[Link]’ >>> import pickle
>>> data = [Link](file) >>> with open(‘pickled_fruit.pkl’, ‘rb’) as file:
Importing Data
>>> df_sheet2 = [Link](‘1960-1966’, pickled_data = [Link](file)
skiprows=[0],
names=[‘Country’,
‘AAM: War(2002)’])
>>> df_sheet1 = [Link](0,
parse_cols=[0],
skiprows=[0], HDF5 Files
names=[‘Country’])
Learn Python for Data Science Interactively To access the sheet names, use the sheet_names attribute:
>>> import h5py
>>> filename = ‘H-H1_LOSC_4_v1-815411200-4096.hdf5’
>>> data = [Link](filename, ‘r’)
>>> data.sheet_names
Importing Data in Python
Most of the time, you’ll use either NumPy or pandas to import
your data:
>>> import numpy as np
SAS Files Matlab Files
>>> import pandas as pd
>>> from sas7bdat import SAS7BDAT
>>> with SAS7BDAT(‘urbanpop.sas7bdat’) as file: >>> import [Link]
df_sas = file.to_data_frame() >>> filename = ‘[Link]’
>>> mat = [Link](filename)
Help
>>> [Link]([Link])
>>> help(pd.read_csv)
Stata Files
>>> data = pd.read_stata(‘[Link]’) Exploring Dictionaries
Text Files Accessing Elements with Functions
Relational Databases >>> print([Link]())
>>> for key in [Link]():
Print dictionary keys
Print dictionary keys
Plain Text Files print(key)
>>> from sqlalchemy import create_engine meta
>>> filename = ‘huck_finn.txt’ >>> engine = create_engine(‘sqlite://[Link]’) quality
>>> file = open(filename, mode=’r’) Open the file for reading strain
>>> text = [Link]() Read a file’s contents >>> pickled_data.values() Return dictionary values
>>> print([Link]) Check whether file is closed Use the table_names() method to fetch a list of table names: >>> print([Link]()) Returns items in list format
>>> [Link]() Close file of (key, value)tuple pairs
>>> print(text) >>> table_names = engine.table_names()
Using the context manager with Accessing Data Items with Keys
Querying Relational Databases
>>> with open(‘huck_finn.txt’, ‘r’) as file:
>>> con = [Link]() >>> for key in data [‘meta’].keys() Explore the HDF5 structure
print([Link]()) Read a single line
>>> rs = [Link](“SELECT * FROM Orders”) print(key)
print([Link]())
>>> df = [Link]([Link]()) Description
print([Link]())
>>> [Link] = [Link]() DescriptionURL
>>> [Link]() Detector
Duration
GPSstart
Using the context manager with Observatory
Table Data: Flat Files Type
>>> with [Link]() as con:
UTCstart
rs = [Link](“SELECT OrderID FROM Orders”)
Importing Flat Files with numpy >>> print(data[‘meta’][‘Description’].value) Retrieve the value for a key
df = [Link]([Link](size=5))
Files with one data type [Link] = [Link]()
>>> filename = ‘[Link]’
>>> data = [Link](filename,
Querying relational databases with pandas
Navigating Your FileSystem
delimiter=’,’, String used to separate values
skiprows=2, Skip the first 2 lines
usecols=[0,2], Read the 1st and 3rd column >>> df = pd.read_sql_query(“SELECT * FROM Orders”, engine)
dtype=str) The type of the resulting array Magic Commands
Files with mixed data types !ls List directory contents of files and directories
%cd .. Change current working directory
>>> filename = ‘[Link]’ Exploring Your Data %pwd Return the current working directory path
>>> data = [Link](filename,
delimiter=’,’,
names=True, Look for column header NumPy Arrays
dtype=None) os Library
>>> data_array.dtype Data type of array elements
>>> data_array.shape Array dimensions >>> import os
>>> data_array = [Link](filename) >>> len(data_array) Length of array >>> path = “/usr/tmp”
>>> wd = [Link]() Store the name of current
directory in a string
Importing Flat Files with numpy pandas DataFrames >>> [Link](wd) Output contents of the di
rectory in a list
>>> filename = ‘[Link]’ >>> [Link]() Return first DataFrame rows >>> [Link](path) Change current working
>>> data = pd.read_csv(filename, >>> [Link]() Return last DataFrame rows directory
nrows=5, Number of rows of file to read >>> [Link] Describe index >>> [Link](“[Link]”, Rename a file
header=None, Row number to use as col names >>> [Link] Describe DataFrame columns “[Link]”)
sep=’\t’, Delimiter to use >>> [Link]() Info on DataFrame >>> [Link](“[Link]”) Delete an existing file
comment=’#’, Character to split comments >>> data_array = [Link] Convert a DataFrame to an a >>> [Link](“newdir”) Create a new directory
n a_values=[“”]) String to recognize as NA/NaN NumPy array