Tuesday, November 14, 2017

List Comprehensions in Python

List comprehensions provide a concise way to create lists. It consists of brackets containing an expression followed by a for clause, then zero or more for or if clauses. The expressions can be anything, meaning you can put in all kinds of objects in lists.

The result will be a new list resulting from evaluating the expression in the context of the for and if clauses which follow it. 

The list comprehension always returns a result list. 

If you used to do it like this:
new_list = []
for i in old_list:
    if filter(i):
        new_list.append(expressions(i))

You can obtain the same thing using list comprehension:
new_list = [expression(i) for i in old_list if filter(i)]

Examples

x = [i for i in range(10)]
print x
# This will give the output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

For the next example, assume we want to create a list of squares.

# You can either use loops:
squares = []

for x in range(10):
    squares.append(x**2)
 
print squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

# Or you can use list comprehensions to get the same result:
squares = [x**2 for x in range(10)]

# print squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

Wednesday, September 6, 2017

Python Simple Heat Map

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

a = np.random.random((5, 4))
df = pd.DataFrame({'a': a[0], 'b': a[1], 'c': a[2], 'd': a[3], 'e': a[4]})
rows = list('abcd')
columns = list(df.columns)

fig = plt.figure(figsize=(10,10), dpi=72) 
ax = fig.add_subplot(111) 
plt.pcolor(df, cmap = plt.cm.Reds) # cmap='hot'

ax.set_xticks(np.arange(0,5)+0.5)
ax.set_yticks(np.arange(0,4)+0.5)
ax.set_yticklabels(rows)
ax.set_xticklabels(columns)
plt.show()

Friday, September 1, 2017

Python pandas iloc vs ix vs loc explanation

"""
loc is label based indexing so basically looking up a value in a row
iloc is integer row based indexing
ix is a general method that first performs label based, if that fails then it falls to integer based
"""
import pandas as pd

df = pd.DataFrame({'A': ['abc', 'xyz', 'pqr'], 'B': [25, 50, 75]}, index = [50, 100, 150])
print(df)

print(df.loc[100, :])  # subset dataframe df if index value = 100 across all columns

print(df.iloc[1, :])   # subset dataframe df for row = 1 across all columns

df = pd.DataFrame({'A': ['abc', 'xyz', 'pqr'], 'B': [25, 50, 75]}, index = ['50', '100', '150'])

# the following yield same result
print(df.ix['50', :])
print(df.ix[1, :])