Python For Data Science Cheat Sheet Lists Also see NumPy ...
Python For Data Science Cheat Sheet
Python Basics
Learn More Python for Data Science Interactively at
Lists
>>>
>>>
>>>
>>>
Also see NumPy Arrays
a = 'is'
b = 'nice'
my_list = ['my', 'list', a, b]
my_list2 = [[4,5,6,7], [3,4,5,6]]
Selecting List Elements
Variables and Data Types
Subset
Variable Assignment
>>> x=5
>>> x
5
>>> my_list[1]
>>> my_list[-3]
Select item at index 1
Select 3rd last item
>>>
>>>
>>>
>>>
Select items at index 1 and 2
Select items after index 0
Select items before index 3
Copy my_list
Slice
Calculations With Variables
>>> x+2
Sum of two variables
>>> x-2
Subtraction of two variables
>>> x*2
Multiplication of two variables
>>> x**2
Exponentiation of a variable
>>> x%2
Remainder of a variable
>>> x/float(2)
Division of a variable
7
3
10
25
1
Index starts at 0
2.5
my_list[1:3]
my_list[1:]
my_list[:3]
my_list[:]
Subset Lists of Lists
>>> my_list2[1][0]
>>> my_list2[1][:2]
my_list[list][itemOfList]
str()
'5', '3.45', 'True'
Variables to strings
int()
5, 3, 1
Variables to integers
>>> my_list + my_list
['my', 'list', 'is', 'nice', 'my', 'list', 'is', 'nice']
>>> my_list * 2
['my', 'list', 'is', 'nice', 'my', 'list', 'is', 'nice']
>>> my_list2 > 4
float()
5.0, 1.0
Variables to floats
bool()
True, True, True
Variables to booleans
Asking For Help
>>> help(str)
my_list.index(a)
my_list.count(a)
my_list.append('!')
my_list.remove('!')
del(my_list[0:1])
my_list.reverse()
my_list.extend('!')
my_list.pop(-1)
my_list.insert(0,'!')
my_list.sort()
'thisStringIsAwesome'
String Operations
>>> my_string * 2
'thisStringIsAwesomethisStringIsAwesome'
>>> my_string + 'Innit'
'thisStringIsAwesomeInnit'
>>> 'm' in my_string
True
Machine learning
Scientific computing
2D plotting
Free IDE that is included
with Anaconda
Leading open data science platform
powered by Python
Create and share
documents with live code,
visualizations, text, ...
Numpy Arrays
Also see Lists
Selecting Numpy Array Elements
Subset
>>> my_array[1]
Get the index of an item
Count an item
Append an item at a time
Remove an item
Remove an item
Reverse the list
Append an item
Remove an item
Insert an item
Sort the list
Index starts at 0
Select item at index 1
Slice
>>> my_array[0:2]
Select items at index 0 and 1
array([1, 2])
Subset 2D Numpy arrays
>>> my_2darray[:,0]
my_2darray[rows, columns]
array([1, 4])
Numpy Array Operations
>>> my_array > 3
array([False, False, False,
>>> my_array * 2
True], dtype=bool)
array([2, 4, 6, 8])
>>> my_array + np.array([5, 6, 7, 8])
array([6, 8, 10, 12])
Strings
>>> my_string = 'thisStringIsAwesome'
>>> my_string
Data analysis
Install Python
2
List Methods
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
Import libraries
>>> import numpy
>>> import numpy as np
Selective import
>>> from math import pi
>>> my_list = [1, 2, 3, 4]
>>> my_array = np.array(my_list)
>>> my_2darray = np.array([[1,2,3],[4,5,6]])
List Operations
True
Types and Type Conversion
Libraries
String Operations
Index starts at 0
>>> my_string[3]
>>> my_string[4:9]
String Methods
>>>
>>>
>>>
>>>
>>>
String to uppercase
my_string.upper()
String to lowercase
my_string.lower()
Count String elements
my_string.count('w')
my_string.replace('e', 'i') Replace String elements
my_string.strip()
Strip whitespaces
Numpy Array Functions
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
my_array.shape
np.append(other_array)
np.insert(my_array, 1, 5)
np.delete(my_array,[1])
np.mean(my_array)
np.median(my_array)
my_array.corrcoef()
np.std(my_array)
DataCamp
Get the dimensions of the array
Append items to an array
Insert items in an array
Delete items in an array
Mean of the array
Median of the array
Correlation coefficient
Standard deviation
Learn Python for Data Science Interactively
Python For Data Science Cheat Sheet
Jupyter Notebook
Working with Different Programming Languages
Widgets
Kernels provide computation and communication with front-end interfaces
like the notebooks. There are three main kernels:
Notebook widgets provide the ability to visualize and control changes
in your data, often as a control like a slider, textbox, etc.
Learn More Python for Data Science Interactively at
IRkernel
Installing Jupyter Notebook will automatically install the IPython kernel.
Saving/Loading Notebooks
Save current notebook
and record checkpoint
Preview of the printed
notebook
Close notebook & stop
running any scripts
Open an existing
notebook
Rename notebook
Revert notebook to a
previous checkpoint
You can use them to build interactive GUIs for your notebooks or to
synchronize stateful and stateless information between Python and
JavaScript.
Interrupt kernel
Restart kernel
Create new notebook
Make a copy of the
current notebook
IJulia
Interrupt kernel &
clear all output
Restart kernel & run
all cells
Connect back to a
remote notebook
Restart kernel & run
all cells
Download serialized
state of all widget
models in use
Save notebook
with interactive
widgets
Embed current
widgets
Run other installed
kernels
Command Mode:
Download notebook as
- IPython notebook
- Python
- HTML
- Markdown
- reST
- LaTeX
- PDF
15
13
1
2
3
4
5
6
7
8
9
10
11
14
12
Writing Code And Text
Code and text are encapsulated by 3 basic cell types: markdown cells, code
cells, and raw NBConvert cells.
Edit Cells
Cut currently selected cells
to clipboard
Paste cells from
clipboard above
current cell
Paste cells from
clipboard on top
of current cel
Revert ¡°Delete Cells¡±
invocation
Copy cells from
clipboard to current
cursor position
Paste cells from
clipboard below
current cell
Delete current cells
Split up a cell from
current cursor
position
Merge current cell
with the one above
Merge current cell
with the one below
Move current cell up
Move current cell
down
Adjust metadata
underlying the
current notebook
Remove cell
attachments
Paste attachments of
current cell
Find and replace
in selected cells
Copy attachments of
current cell
Insert image in
selected cells
Edit Mode:
Executing Cells
Run selected cell(s)
Run current cells down
and create a new one
above
Add new cell below the
current one
Run all cells
Run all cells above the
current cell
Run all cells below
the current cell
Change the cell type of
current cell
toggle, toggle
scrolling and clear
current outputs
toggle, toggle
scrolling and clear
all output
Toggle display of Jupyter
logo and filename
Toggle line numbers
in cells
Walk through a UI tour
List of built-in keyboard
shortcuts
Edit the built-in
keyboard shortcuts
Notebook help topics
Description of
markdown available
in notebook
Information on
unofficial Jupyter
Notebook extensions
IPython help topics
NumPy help topics
Toggle display of toolbar
Toggle display of cell
action icons:
- None
- Edit metadata
- Raw cell format
- Slideshow
- Attachments
- Tags
9. Interrupt kernel
10. Restart kernel
11. Display characteristics
12. Open command palette
13. Current kernel
14. Kernel status
15. Log out from notebook server
Asking For Help
Python help topics
View Cells
Insert Cells
Add new cell above the
current one
Run current cells down
and create a new one
below
1. Save and checkpoint
2. Insert cell below
3. Cut cell
4. Copy cell(s)
5. Paste cell(s) below
6. Move cell up
7. Move cell down
8. Run current cell
SciPy help topics
Matplotlib help topics
SymPy help topics
Pandas help topics
About Jupyter Notebook
DataCamp
Learn Python for Data Science Interactively
Python For Data Science Cheat Sheet
NumPy Basics
Learn Python for Data Science Interactively at
NumPy
2
The NumPy library is the core library for scientific computing in
Python. It provides a high-performance multidimensional array
object, and tools for working with these arrays.
>>> import numpy as np
NumPy Arrays
1
2
3
2D array
3D array
axis 1
axis 0
1.5
2
3
4
5
6
axis 2
axis 1
axis 0
>>> a = np.array([1,2,3])
>>> b = np.array([(1.5,2,3), (4,5,6)], dtype = float)
>>> c = np.array([[(1.5,2,3), (4,5,6)], [(3,2,1), (4,5,6)]],
dtype = float)
Initial Placeholders
>>> np.zeros((3,4))
Create an array of zeros
>>> np.ones((2,3,4),dtype=np.int16) Create an array of ones
>>> d = np.arange(10,25,5)
Create an array of evenly
spaced values (step value)
>>> np.linspace(0,2,9)
Create an array of evenly
spaced values (number of samples)
>>> e = np.full((2,2),7)
Create a constant array
>>> f = np.eye(2)
Create a 2X2 identity matrix
>>> np.random.random((2,2))
Create an array with random values
>>> np.empty((3,2))
Create an empty array
I/O
Saving & Loading On Disk
>>> np.save('my_array', a)
>>> np.savez('array.npz', a, b)
>>> np.load('my_array.npy')
Saving & Loading Text Files
>>> np.loadtxt("myfile.txt")
>>> np.genfromtxt("my_file.csv", delimiter=',')
>>> np.savetxt("myarray.txt", a, delimiter=" ")
Data Types
np.int64
np.float32
plex
np.bool
np.object
np.string_
np.unicode_
a.shape
len(a)
b.ndim
e.size
b.dtype
b.dtype.name
b.astype(int)
Subsetting, Slicing, Indexing
Array dimensions
Length of array
Number of array dimensions
Number of array elements
Data type of array elements
Name of data type
Convert an array to a different type
Signed 64-bit integer types
Standard double-precision floating point
Complex numbers represented by 128 floats
Boolean type storing TRUE and FALSE values
Python object type
Fixed-length string type
Fixed-length unicode type
Subsetting
Asking For Help
1
2
3
Select the element at the 2nd index
>>> b[1,2]
6.0
1.5
2
3
4
5
6
Select the element at row 0 column 2
(equivalent to b[1][2])
>>> a[0:2]
1
2
3
Select items at index 0 and 1
1.5
2
3
Select items at rows 0 and 1 in column 1
4
5
6
1.5
2
3
4
5
6
3
Slicing
>>> b[0:2,1]
>>> (np.ndarray.dtype)
array([ 2.,
Array Mathematics
Also see Lists
>>> a[2]
array([1, 2])
5.])
>>> b[:1]
>>> c[1,...]
Select all items at row 0
(equivalent to b[0:1, :])
Same as [1,:,:]
>>> a[ : :-1]
Reversed array a
array([[1.5, 2., 3.]])
array([[[ 3., 2., 1.],
[ 4., 5., 6.]]])
>>> g = a - b
Subtraction
>>> np.subtract(a,b)
>>> b + a
Subtraction
Addition
>>> a[a>> np.add(b,a)
>>> a / b
Addition
Division
>>> b[[1, 0, 1, 0],[0, 1, 2, 0]]
Select elements (1,0),(0,1),(1,2) and (0,0)
>>> b[[1, 0, 1, 0]][:,[0,1,2,0]]
Select a subset of the matrix¡¯s rows
and columns
array([[-0.5, 0. , 0. ],
[-3. , -3. , -3. ]])
array([[ 2.5,
[ 5. ,
4. ,
7. ,
array([[ 0.66666667, 1.
[ 0.25
, 0.4
array([[
[
>>>
>>>
>>>
>>>
>>>
>>>
>>>
1.5,
4. ,
4. ,
10. ,
np.multiply(a,b)
np.exp(b)
np.sqrt(b)
np.sin(a)
np.cos(b)
np.log(a)
e.dot(f)
array([[ 7.,
[ 7.,
array([3, 2, 1])
6. ],
9. ]])
>>> np.divide(a,b)
>>> a * b
Creating Arrays
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
Arithmetic Operations
Use the following import convention:
1D array
Inspecting Your Array
, 1.
, 0.5
],
]])
9. ],
18. ]])
Division
Multiplication
Multiplication
Exponentiation
Square root
Print sines of an array
Element-wise cosine
Element-wise natural logarithm
Dot product
7.],
7.]])
>>> a == b
Element-wise comparison
>>> a < 2
Element-wise comparison
>>> np.array_equal(a, b)
Array-wise comparison
array([[False, True, True],
[False, False, False]], dtype=bool)
array([True, False, False], dtype=bool)
Aggregate Functions
a.sum()
a.min()
b.max(axis=0)
b.cumsum(axis=1)
a.mean()
b.median()
a.corrcoef()
np.std(b)
Array-wise sum
Array-wise minimum value
Maximum value of an array row
Cumulative sum of the elements
Mean
Median
Correlation coefficient
Standard deviation
Copying Arrays
>>> h = a.view()
>>> np.copy(a)
>>> h = a.copy()
2
3
Fancy Indexing
array([ 4. , 2. , 6. , 1.5])
array([[ 4. ,5.
[ 1.5, 2.
[ 4. , 5.
[ 1.5, 2.
,
,
,
,
6.
3.
6.
3.
,
,
,
,
4. ],
1.5],
4. ],
1.5]])
Create a view of the array with the same data
Create a copy of the array
Create a deep copy of the array
Sort an array
Sort the elements of an array's axis
Select elements from a less than 2
Array Manipulation
Transposing Array
>>> i = np.transpose(b)
>>> i.T
Permute array dimensions
Permute array dimensions
>>> b.ravel()
>>> g.reshape(3,-2)
Flatten the array
Reshape, but don¡¯t change data
>>>
>>>
>>>
>>>
Return a new array with shape (2,6)
Append items to an array
Insert items in an array
Delete items from an array
Changing Array Shape
h.resize((2,6))
np.append(h,g)
np.insert(a, 1, 5)
np.delete(a,[1])
Combining Arrays
>>> np.concatenate((a,d),axis=0) Concatenate arrays
array([ 1,
2,
3, 10, 15, 20])
>>> np.vstack((a,b))
Stack arrays vertically (row-wise)
>>> np.r_[e,f]
>>> np.hstack((e,f))
Stack arrays vertically (row-wise)
Stack arrays horizontally (column-wise)
array([[ 1. ,
[ 1.5,
[ 4. ,
array([[ 7.,
[ 7.,
2. ,
2. ,
5. ,
7.,
7.,
3. ],
3. ],
6. ]])
1.,
0.,
0.],
1.]])
>>> np.column_stack((a,d))
Create stacked column-wise arrays
>>> np.c_[a,d]
Create stacked column-wise arrays
>>> np.hsplit(a,3)
Split the array horizontally at the 3rd
index
Split the array vertically at the 2nd index
array([[ 1, 10],
[ 2, 15],
[ 3, 20]])
Splitting Arrays
Sorting Arrays
>>> a.sort()
>>> c.sort(axis=0)
1
array([1])
Adding/Removing Elements
Comparison
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
Boolean Indexing
[array([1]),array([2]),array([3])]
>>> np.vsplit(c,2)
[array([[[ 1.5,
[ 4. ,
array([[[ 3.,
[ 4.,
2. , 1. ],
5. , 6. ]]]),
2., 3.],
5., 6.]]])]
DataCamp
Learn Python for Data Science Interactively
Python For Data Science Cheat Sheet
SciPy - Linear Algebra
Learn More Python for Data Science Interactively at
SciPy
The SciPy library is one of the core packages for
scientific computing that provides mathematical
algorithms and convenience functions built on the
NumPy extension of Python.
Interacting With NumPy
>>>
>>>
>>>
>>>
Also see NumPy
import numpy as np
a = np.array([1,2,3])
b = np.array([(1+5j,2j,3j), (4j,5j,6j)])
c = np.array([[(1.5,2,3), (4,5,6)], [(3,2,1), (4,5,6)]])
Index Tricks
>>>
>>>
>>>
>>>
np.mgrid[0:5,0:5]
np.ogrid[0:2,0:2]
np.r_[[3,[0]*5,-1:1:10j]
np.c_[b,c]
Create a dense meshgrid
Create an open meshgrid
Stack arrays vertically (row-wise)
Create stacked column-wise arrays
Shape Manipulation
>>>
>>>
>>>
>>>
>>>
>>>
np.transpose(b)
b.flatten()
np.hstack((b,c))
np.vstack((a,b))
np.hsplit(c,2)
np.vpslit(d,2)
Permute array dimensions
Flatten the array
Stack arrays horizontally (column-wise)
Stack arrays vertically (row-wise)
Split the array horizontally at the 2nd index
Split the array vertically at the 2nd index
Polynomials
>>> from numpy import poly1d
>>> p = poly1d([3,4,5])
Create a polynomial object
Vectorizing Functions
>>> np.vectorize(myfunc)
Vectorize functions
Type Handling
>>>
>>>
>>>
>>>
np.real(c)
np.imag(c)
np.real_if_close(c,tol=1000)
np.cast['f'](np.pi)
Return the real part of the array elements
Return the imaginary part of the array elements
Return a real array if complex parts close to 0
Cast object to a data type
Other Useful Functions
(number of samples)
>>>
>>>
>>>
>>>
g [3:] += np.pi
np.unwrap(g)
Unwrap
np.logspace(0,10,3)
Create an array of evenly spaced values (log scale)
np.select([c>>
>>>
>>>
>>>
misc.factorial(a)
b(10,3,exact=True)
misc.central_diff_weights(3)
misc.derivative(myfunc,1.0)
>>> from scipy import linalg, sparse
Creating Matrices
>>>
>>>
>>>
>>>
A
B
C
D
=
=
=
=
Basic Matrix Routines
Inverse
>>>
>>>
>>>
>>>
>>>
A.I
linalg.inv(A)
A.T
A.H
np.trace(A)
Norm
>>> linalg.norm(A)
>>> linalg.norm(A,1)
>>> linalg.norm(A,np.inf)
Rank
conditions
Factorial
Combine N things taken at k time
Weights for Np-point central derivative
Find the n-th derivative of a function at a point
Inverse
Inverse
Tranpose matrix
Conjugate transposition
Trace
Frobenius norm
L1 norm (max column sum)
L inf norm (max row sum)
Matrix rank
>>> linalg.det(A)
Determinant
>>> linalg.solve(A,b)
>>> E = np.mat(a).T
>>> linalg.lstsq(D,E)
Solver for dense matrices
Solver for dense matrices
Least-squares solution to linear matrix
equation
Solving linear problems
Generalized inverse
>>> linalg.pinv(C)
Compute the pseudo-inverse of a matrix
(least-squares solver)
Compute the pseudo-inverse of a matrix
(SVD)
F = np.eye(3, k=1)
G = np.mat(np.identity(2))
C[C > 0.5] = 0
H = sparse.csr_matrix(C)
I = sparse.csc_matrix(D)
J = sparse.dok_matrix(A)
E.todense()
sparse.isspmatrix_csc(A)
Create a 2X2 identity matrix
Create a 2x2 identity matrix
Compressed Sparse Row matrix
Compressed Sparse Column matrix
Dictionary Of Keys matrix
Sparse matrix to full matrix
Identify sparse matrix
Sparse Matrix Routines
Inverse
Inverse
>>> sparse.linalg.norm(I)
Norm
>>> sparse.linalg.spsolve(H,I)
Solver for sparse matrices
Sparse Matrix Functions
>>> sparse.linalg.expm(I)
Asking For Help
>>> help(scipy.linalg.diagsvd)
>>> (np.matrix)
>>> np.subtract(A,D)
Subtraction
>>> np.divide(A,D)
Division
>>>
>>>
>>>
>>>
>>>
>>>
>>>
Multiplication
Dot product
Vector dot product
Inner product
Outer product
Tensor dot product
Kronecker product
Division
np.multiply(D,A)
np.dot(A,D)
np.vdot(A,D)
np.inner(A,D)
np.outer(A,D)
np.tensordot(A,D)
np.kron(A,D)
Exponential Functions
>>> linalg.expm(A)
>>> linalg.expm2(A)
>>> linalg.expm3(D)
Matrix exponential
Matrix exponential (Taylor Series)
Matrix exponential (eigenvalue
decomposition)
Logarithm Function
>>> linalg.logm(A)
Matrix logarithm
>>> linalg.sinm(D)
>>> linalg.cosm(D)
>>> linalg.tanm(A)
Matrix sine
Matrix cosine
Matrix tangent
>>> linalg.sinhm(D)
>>> linalg.coshm(D)
>>> linalg.tanhm(A)
Hypberbolic matrix sine
Hyperbolic matrix cosine
Hyperbolic matrix tangent
>>> np.sigm(A)
Matrix sign function
>>> linalg.sqrtm(A)
Matrix square root
>>> linalg.funm(A, lambda x: x*x)
Evaluate matrix function
Trigonometric Tunctions
Hyperbolic Trigonometric Functions
Matrix Sign Function
Matrix Square Root
Decompositions
Eigenvalues and Eigenvectors
>>> la, v = linalg.eig(A)
>>>
>>>
>>>
>>>
l1, l2 = la
v[:,0]
v[:,1]
linalg.eigvals(A)
Singular Value Decomposition
>>> sparse.linalg.inv(I)
Solving linear problems
Addition
Subtraction
Arbitrary Functions
Creating Sparse Matrices
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> np.add(A,D)
Multiplication
>>> np.linalg.matrix_rank(C)
Determinant
Matrix Functions
Addition
np.matrix(np.random.random((2,2)))
np.asmatrix(b)
np.mat(np.random.random((10,5)))
np.mat([[3,4], [5,6]])
Norm
>>> np.angle(b,deg=True)
Return the angle of the complex argument
>>> g = np.linspace(0,np.pi,num=5) Create an array of evenly spaced values
Also see NumPy
You¡¯ll use the linalg and sparse modules. Note that scipy.linalg contains and expands on numpy.linalg.
>>> linalg.pinv2(C)
>>> def myfunc(a):
if a < 0:
return a*2
else:
return a/2
Linear Algebra
Sparse matrix exponential
Solve ordinary or generalized
eigenvalue problem for square matrix
Unpack eigenvalues
First eigenvector
Second eigenvector
Unpack eigenvalues
>>> U,s,Vh = linalg.svd(B)
Singular Value Decomposition (SVD)
>>> M,N = B.shape
>>> Sig = linalg.diagsvd(s,M,N) Construct sigma matrix in SVD
LU Decomposition
>>> P,L,U = linalg.lu(C)
LU Decomposition
Sparse Matrix Decompositions
>>> la, v = sparse.linalg.eigs(F,1)
>>> sparse.linalg.svds(H, 2)
DataCamp
Eigenvalues and eigenvectors
SVD
Learn Python for Data Science Interactively
Python For Data Science Cheat Sheet
Pandas Basics
Learn Python for Data Science Interactively at
Asking For Help
Selection
Also see NumPy Arrays
Getting
>>> s['b']
Get one element
>>> df[1:]
Get subset of a DataFrame
-5
Pandas
The Pandas library is built on NumPy and provides easy-to-use
data structures and data analysis tools for the Python
programming language.
Dropping
>>> help(pd.Series.loc)
1
2
Country
India
Brazil
Capital
New Delhi
Bras¨ªlia
Population
1303171035
207847528
By Position
>>> import pandas as pd
>>> df.iloc([0],[0])
'Belgium'
Pandas Data Structures
Index
a
3
b
-5
c
7
d
4
>>> s = pd.Series([3, -5, 7, 4], index=['a', 'b', 'c', 'd'])
DataFrame
Columns
Index
Select single value by row &
column
'Belgium'
A one-dimensional labeled array
capable of holding any data type
Country
0
Belgium
1
India
2
Brazil
Capital
Brussels
Population
11190846
New Delhi 1303171035
Bras¨ªlia
A two-dimensional labeled
data structure with columns
of potentially different types
207847528
>>> data = {'Country': ['Belgium', 'India', 'Brazil'],
'Capital': ['Brussels', 'New Delhi', 'Bras¨ªlia'],
'Population': [11190846, 1303171035, 207847528]}
>>> df = pd.DataFrame(data,
columns=['Country', 'Capital', 'Population'])
'Belgium'
Select single value by row &
column labels
>>> df.at([0], ['Country'])
'Belgium'
By Label/Position
>>> df.ix[2]
Select single row of
subset of rows
>>> df.ix[:,'Capital']
Select a single column of
subset of columns
>>> df.ix[1,'Capital']
Select rows and columns
Country
Brazil
Capital
Bras¨ªlia
Population 207847528
0
1
2
Brussels
New Delhi
Bras¨ªlia
Boolean Indexing
Setting
Set index a of Series s to 6
Read and Write to Excel
>>> pd.read_excel('file.xlsx')
>>> pd.to_excel('dir/myDataFrame.xlsx', sheet_name='Sheet1')
Read multiple sheets from the same file
>>> xlsx = pd.ExcelFile('file.xls')
>>> df = pd.read_excel(xlsx, 'Sheet1')
(rows,columns)
Describe index
Describe DataFrame columns
Info on DataFrame
Number of non-NA values
>>>
>>>
>>>
>>>
>>>
>>>
>>>
df.sum()
df.cumsum()
df.min()/df.max()
df.idxmin()/df.idxmax()
df.describe()
df.mean()
df.median()
Sum of values
Cummulative sum of values
Minimum/maximum values
Minimum/Maximum index value
Summary statistics
Mean of values
Median of values
Applying Functions
>>> f = lambda x: x*2
>>> df.apply(f)
>>> df.applymap(f)
Apply function
Apply function element-wise
Internal Data Alignment
>>> s3 = pd.Series([7, -2, 3], index=['a', 'c', 'd'])
>>> s + s3
a
10.0
c
5.0
b
d
NaN
7.0
Arithmetic Operations with Fill Methods
I/O
>>> pd.read_csv('file.csv', header=None, nrows=5)
>>> df.to_csv('myDataFrame.csv')
df.shape
df.index
df.columns
()
df.count()
NA values are introduced in the indices that don¡¯t overlap:
>>> s[~(s > 1)]
Series s where value is not >1
>>> s[(s < -1) | (s > 2)]
s where value is 2
>>> df[df['Population']>1200000000] Use filter to adjust DataFrame
Read and Write to CSV
>>>
>>>
>>>
>>>
>>>
Data Alignment
'New Delhi'
>>> s['a'] = 6
>>> df.sort_index()
Sort by labels along an axis
>>> df.sort_values(by='Country') Sort by the values along an axis
>>> df.rank()
Assign ranks to entries
Summary
By Label
>>> df.loc([0], ['Country'])
Sort & Rank
Basic Information
>>> df.iat([0],[0])
Series
Drop values from rows (axis=0)
>>> df.drop('Country', axis=1) Drop values from columns(axis=1)
Retrieving Series/DataFrame Information
Selecting, Boolean Indexing & Setting
Use the following import convention:
>>> s.drop(['a', 'c'])
Read and Write to SQL Query or Database Table
>>>
>>>
>>>
>>>
>>>
from sqlalchemy import create_engine
engine = create_engine('sqlite:///:memory:')
pd.read_sql("SELECT * FROM my_table;", engine)
pd.read_sql_table('my_table', engine)
pd.read_sql_query("SELECT * FROM my_table;", engine)
read_sql()is a convenience wrapper around read_sql_table() and
read_sql_query()
>>> pd.to_sql('myDf', engine)
You can also do the internal data alignment yourself with
the help of the fill methods:
>>> s.add(s3, fill_value=0)
a
b
c
d
10.0
-5.0
5.0
7.0
>>> s.sub(s3, fill_value=2)
>>> s.div(s3, fill_value=4)
>>> s.mul(s3, fill_value=3)
DataCamp
Learn Python for Data Science Interactively
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- cheat sheet numpy python copy
- an introduction to numpy and scipy
- python for data science cheat sheet lists also see numpy
- use python with r with reticulate cheat sheet
- mathematics in python
- numpy user guide
- chapter data handling using 2 pandas i
- matplotlib rxjs ggplot2 python data persistence
- preeti arora
- python pandas tutorial rxjs ggplot2 python data
Related searches
- cheat sheet for word brain game
- grammar cheat sheet for kids
- cheat sheet for english grammar
- cheat sheet for words with friends
- latest cheat sheet for scrabble
- python cheat sheet pdf
- python functions cheat sheet pdf
- python cheat sheet class
- python cheat sheet pdf basics
- python cheat sheet for beginners
- beginners python cheat sheet pdf
- python cheat sheet download