Python For Data Science Cheat Sheet

Pandas Basics

https://www.datacamp.com/community/blog/python-pandas-cheat-sheet

Pandas

The Pandas library is built on NumPy and provides easy-to-use data structures and data analysis tools for the Python programming language.

Use the following import convention:

In [ ]:
 import pandas as pd
In [ ]:
pd.__version__

Pandas Data Structures

Series

A one-dimensional labeled array A capable of holding any data type

In [ ]:
s = pd.Series([3, -5, 7, 4], index=['a', 'b', 'c', 'd'])
In [ ]:
s

DataFrame

A two-dimensional labeled data structure with columns of potentially different types

In [ ]:
data = {'Country': ['Belgium', 'India', 'Brazil'],
              'Capital': ['Brussels', 'New Delhi', 'Brasilia'],
              'Population': [11190846, 1303171035, 207847528]}
In [ ]:
df = pd.DataFrame(data,
                                  columns=['Country', 'Capital', 'Population'])
In [ ]:
df

Asking For Help

In [ ]:
help(pd.Series.loc)

Selection (Also see NumPy Arrays)

Getting

In [ ]:
# Get one element
s['b']
In [ ]:
# Get subset of a DataFrame
df[1:]

Selecting, Boolean Indexing & Setting

By Position

In [ ]:
## Select single value by row & column
df.iloc[0, 0]
In [ ]:
# Select single value by row & column
df.iat[0, 0]

By Label

In [ ]:
# Select single value by row & column labels
df.loc[0, 'Country']
In [ ]:
# Select single value by row & column labels
df.at[0, 'Country']

By Label/Position

In [ ]:
# Select single row of subset of rows
df.ix[2]
In [ ]:
# Select a single column of subset of columns 
df.ix[:,'Capital']
In [ ]:
# Select rows and columns
df.ix[1,'Capital']

Boolean Indexing

In [ ]:
# Series s where value is not >1
s[~(s > 1)]
In [ ]:
# s where value is <-1 or >2
s[(s < -1) | (s > 2)]
In [ ]:
# Use filter to adjust DataFrame
df[df['Population'] > 1200000000] 

Setting

In [ ]:
# Set index a of Series s to 6
s['a'] = 6

Dropping

In [ ]:
# Drop values from rows (axis=0)
s.drop(['a', 'c'])
In [ ]:
# Drop values from columns(axis=1)
df.drop('Country', axis=1)

Sort & Rank

In [ ]:
# Sort by labels along an axis
df.sort_index()
In [ ]:
#  Sort by the values along an axis
df.sort_values(by='Country')
In [ ]:
# Assign ranks to entries
df.rank()

Retrieving Series/DataFrame Information

Basic Information

In [ ]:
# (rows,columns)
df.shape
In [ ]:
# Describe index
df.index
In [ ]:
# Describe DataFrame columns
df.columns
In [ ]:
# Info on DataFrame
df.info()
In [ ]:
# Number of non-NA values
df.count()

Summary

In [ ]:
# Sum of values
df.sum()
In [ ]:
# Cummulative sum of values
df.cumsum()
In [ ]:
# Minimum values
df.min()
In [ ]:
# Maximum values
df.max()
In [ ]:
# Minimum index value
df.idxmin()
In [ ]:
# Maximum index value
df.idxmax()
In [ ]:
# Summary statistics
df.describe()
In [ ]:
# Mean of values
df.mean()
In [ ]:
# Median of values
df.median()

Applying Functions

In [ ]:
f = lambda x: x*2
In [ ]:
# Apply function
df.apply(f)
In [ ]:
# Apply function element-wise
df.applymap(f)

Data Alignment

Internal Data Alignment

NA values are introduced in the indices that don’t overlap:

In [ ]:
s3 = pd.Series([7, -2, 3], index=['a', 'c', 'd'])
s + s3

Arithmetic Operations with Fill Methods

You can also do the internal data alignment yourself with the help of the fill methods:

In [ ]:
s.add(s3, fill_value=0)
In [ ]:
s.sub(s3, fill_value=2)
In [ ]:
s.div(s3, fill_value=4)
In [ ]:
s.mul(s3, fill_value=3)

I/O

Read and Write to CSV

In [ ]:
df.to_csv('myDataFrame.csv')
In [ ]:
pd.read_csv('myDataFrame.csv', header=None, nrows=5)

Read and Write to Excel

In [ ]:
df.to_excel('myDataFrame.xlsx', sheet_name='Sheet1')
In [ ]:
pd.read_excel('myDataFrame.xlsx')

Read and Write to SQL Query or Database Table

In [ ]:
from sqlalchemy import create_engine
engine = create_engine('sqlite:///:memory:')

write records stored in a DataFrame to a SQL database.

In [ ]:
df.to_sql("my_table", engine)

read_sql() is a convenience wrapper around read_sql_table() and read_sql_query()

In [ ]:
pd.read_sql("SELECT * FROM my_table;", engine)
In [ ]:
pd.read_sql_table('my_table', engine)
In [ ]:
pd.read_sql_query("SELECT * FROM my_table;", engine)