4  Useful functions

Code
numbers = (1,2,3,4)
squared_numbers = map(lambda n: n**2, numbers)

# list(squared_numbers) = [1, 4, 9, 16]
Code
def check_even(number):
    if number % 2 == 0:
        return True  

    return False

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = filter(check_even, numbers)

# list(even_numbers) = [2, 4, 6, 8, 10]
Code
from functools import reduce

a = [1, 2, 3, 4]
result = reduce(lambda x,y: x*y, a)

# result = 24
Tip

For map(), filter() and reduce() functions it is more convenient, if possible, to use lambda functions as argument.

Code
list1 = ["a", "b", "c"]
list2 = [1, 2, 3]
dict_result = {key: val for key, val in zip(list1, list2)}

# dict_result = {'a': 1, 'b': 2, 'c': 3}
Tip

Instead of bunch of print statements use logging module.

Code
from functools import partial

def f(a, b, c, x):
    return 1000*a + 100*b + 10*c + x

g = partial(f, 3, 1, 4)

# g(1) = 3141

4.1 Working with files

  • “with open(„some.txt“) as f” takes care of closing the file
  • .write(), .read(), .close(), .readlines()
  • For faster loading it is commonly to use numpy package: np.savetxt(‘some.txt’, np.array(), fmt=‘%.2f’, header=‘..’), np.loadtxt(‘some.txt’)

4.2 Working with dataframes

  • Combining dataframes in pandas: append() (for horizontal stacking) and concat() (for vertical stacking)
  • Identifying missing values: isnull() and isna()
  • Handling missing values: fillna()
  • Creating missing values for known indexes: pd.DataFrame(index=…, columns=…)
  • df.query(): for extracting data with specified conditions
  • When you want to create a new df based on a subset of your initial df, it’s best to use the copy() method
  • Pandas also has str() method
  • Renaming columns: better to use .rename() function
  • Setting index to a column: df.set_index([‘some’]) (it removes column automatically)
  • DataFrame.plot makes plots of Series or DataFrame. There is various kinds of plot to produce and can be assigned in „kind“ parameter. Options are: ‘line’ (default), ‘bar’, ‘barh’, ‘hist’, ‘box’, ‘kde’/‘density’, ‘area’, ‘pie’, ‘scatter’, ‘hexbin’’