Data Academy Toolkit IF statements
PYTHON QUICK REFERENCE Choose between multiple options based on logic. Each option has a condition, and the code
below it is only run if the condition evaluates to True. The first True condition exits the
statement, and no other code is run.
Use this quick reference to look up key bits of Python syntax.
________________________ if var == 0: | The first condition always starts with “if”
print("Zero")
Data types
There are four basic data types in Python: elif var < 0: | Other conditions (0 or more) use “elif”
print("Negative")
● Integer (a whole number)
else: | “else” catches all other possibilities
● Float (a decimal number)
● String (a collection of characters/symbols)
print("Positive")
● Boolean (True/False)
________________________
FOR loops
int("23") | Convert a value to a integer
A FOR loop repeats the same code several times. The loop keeps running until it has gone
str(45) | Convert a value to a string through every item in a collection, or until it has reached the end of a range.
type(0.53) | Check the type of a value
for thing in collection: | Loop once for each item in a container,
________________________ print(thing)
Variables
A named place to store a value that can change. A variable can be used where you would for thing in range(0, 10): | Loop once for each number from 0 to 10
normally use a value, and the value can be replaced with anything you want, as many times print(thing)
as you want..
___________________
num = 1 | Assign a number to a variable
Functions
wrd = "bee" | Assign a string to a variable
A function is a named, repeatable block of code.
num = num + 1 | Overwrite a variable
def square(num): | Define a function with arguments
num += 1 | Increment a variable return num ** 2 | Return a value
________________________ square(13) | Call a function by name.
________________________
DECODED
+44 (0)20 3583 0972 | info@[Link] | [Link]
Data Academy Toolkit
[Link][1:3, 2:4] | Selecting data by index location
PANDAS QUICK REFERENCE [Link]["A":"E"] | Selecting data by index (label)
Use this quick reference to look up key bits of Pandas syntax.
________________________
________________________ Manipulating data
Data sourcing
Methods to change the dataframe.
Load data from a file.
[Link](columns={"old": "new"}, | Rename columns
import pandas as pd | Access the library inplace=True)
[Link]("name", axis=1, | Remove a row or column by name
df = pd.read_csv("[Link]") | Load data from a CSV
inplace=True)
df = pd.read_excel("[Link]", | Load data from a specified Excel sheet
[Link](inplace=True) | Remove rows with any missing values
sheet_name=0)
[Link](value, inplace=True) | Fill missing values with a substitute
________________________
[Link]("old", "new", | Replace one value with another
Overview
inplace=True)
Quick methods/attributes to understand your dataframe.
[Link](2) | Round numeric values
[Link] | Number of rows and columns [Link]() | Flip rows and columns
[Link] | Data types of columns
________________________
[Link]() | Summary statistics Column methods
[Link]().sum() | Number of NA values for each column Methods to explore/alter a specific column.
[Link]() | A combination of the above options
df["col"].nunique() | All the distinct values in the column
________________________ df["col"].unique() | Count the number of unique values
Selecting data df["col"].value_counts() | A count of each distinct value
Accessing different parts of your dataframe.
df["col"].astype(type) | Convert a column to a given type
df["column_name"] | Selecting a single column by name df["col"].apply(func) | Apply a function to each separate value
df[["column_a", "column_b"]] | Selecting multiple columns by name
________________________
DECODED
+44 (0)20 3583 0972 | info@[Link] | [Link]