Skip to content

Instantly share code, notes, and snippets.

View fabsta's full-sized avatar

Fabian Schreiber fabsta

View GitHub Profile

[TOC]

duplicates

table(complete.cases(df)) # returns logical vector 

Imputing

impute missing values with linear regresion

[TOC]

read csv

train <- read.csv("../train.csv", stringsAsFactors = F, row.names = 1)

with row indices

IMPORTS

'generic import' of math module

import math
math.sqrt(25)

import a function

DATA TYPES

determine the type of an object

type(2)         # returns 'int'
type(2.0)       # returns 'float'
type('two')     # returns 'str'
type(True)      # returns 'bool'
type(None)      # returns 'NoneType'

[TOC]

writing file

using pickle

try:
  f = open(pickle_file, 'wb')
  save = {

STRINGS

(properties: iterable, immutable)

create a string

s = str(42)         # convert another data type into a string
s = 'I like you'

examine a string

SETS

  • like dictionaries, but with keys only (no values)
  • properties: unordered, iterable, mutable, can contain multiple data types
  • made up of unique elements (strings, numbers, or tuples)

create an empty set

empty_set = set()

LISTS

properties: ordered, iterable, mutable, can contain multiple data types

create an empty list (two ways)

empty_list = []
empty_list = list()

create a list

TUPLES

  • like lists, but they don't change size
  • properties: ordered, iterable, immutable, can contain multiple data types

create a tuple

digits = (0, 1, 'two')          # create a tuple directly
digits = tuple([0, 1, 'two'])   # create a tuple from a list
zero = (0,)                     # trailing comma is required to indicate it's a tuple

DICTIONARIES

  • properties: unordered, iterable, mutable, can contain multiple data types
  • made up of key-value pairs
  • keys must be unique, and can be strings, numbers, or tuples
  • values can be any type

create an empty dictionary (two ways)

empty_dict = {}