First Flashcards
Type of variable
type(x)
Python lists
y = [‘a’,’b’,’c’]
List of lists
y = [[‘a’,’b’],[‘c,’d’],[‘e’,’f’]]
Subsetting lists
y[6]
Subset last variable in list
y[-1]
List slicing
y[#:#]
[inclusive:exclusive]
Remove from a list
del(y[#])
del()
List copy
When you copy a list, you create a reference not a new list.
To create a new list, you have to slice" x = ['a', 'b', 'c'] y = list(x) or y = x[:]
Find maximum
max()
Round
round(df, #)
Length of a list or string
len()
List in ascending order
sorted()
Find where something is indexed
index()
> y = [‘a’, 1, ‘b’, 2, ‘de’]
y.index(‘b’)
2
Change/add to your list
append()
> y = [‘a’, 1, ‘b’, 2, ‘de’]
y.append(44)
[‘a’, 1, ‘b’, 2, ‘de’, 44]
Make all upper case
string.upper()
Count occurrences of x in a string
string.count(‘x’)
Remove first x of a list to a matched input
list.remove()
> y = [‘a’, 1, ‘b’, 2, ‘de’]
y.remove(1)
[‘a’, ‘b’, 2, ‘de’, 44]
Reverse the order of elements in the list
list.reverse()
Create numpy array
y = np.array(list)
Numpy subsetting
> y = array([1, 3, 5])
> y[1]
3
> y > 3
array[(False, False, True)]
> y[y > 3]
array[(5)]
Numpy dimensions of an 2-D array
df.shape
> y = array([1, 3, 5],
[4, 5, 6])
y.shape
(2, 3) # 2 rows, 3 cols
Numpy Subsetting 2-D array
> y = array([1, 3, 5],
[4, 5, 6])
> y[0][2]
5
> y[0,2]
5
> y[: , 1:2]
array([3, 5],
[5, 6])
> y[1, :]
array([4, 5, 6])
Numpy mean
np.mean()
also subset with
np.mean(df[:, 0])
Numpy median
np.median()
also subset with
np.median(df[:, 0])