First Flashcards

1
Q

Type of variable

A

type(x)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Python lists

A

y = [‘a’,’b’,’c’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

List of lists

A

y = [[‘a’,’b’],[‘c,’d’],[‘e’,’f’]]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Subsetting lists

A

y[6]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Subset last variable in list

A

y[-1]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

List slicing

A

y[#:#]

[inclusive:exclusive]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Remove from a list

A

del(y[#])

del()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

List copy

A

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[:]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Find maximum

A

max()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Round

A

round(df, #)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Length of a list or string

A

len()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

List in ascending order

A

sorted()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Find where something is indexed

A

index()

> y = [‘a’, 1, ‘b’, 2, ‘de’]
y.index(‘b’)
2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Change/add to your list

A

append()

> y = [‘a’, 1, ‘b’, 2, ‘de’]
y.append(44)
[‘a’, 1, ‘b’, 2, ‘de’, 44]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Make all upper case

A

string.upper()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Count occurrences of x in a string

A

string.count(‘x’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Remove first x of a list to a matched input

A

list.remove()

> y = [‘a’, 1, ‘b’, 2, ‘de’]
y.remove(1)
[‘a’, ‘b’, 2, ‘de’, 44]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Reverse the order of elements in the list

A

list.reverse()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Create numpy array

A

y = np.array(list)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Numpy subsetting

A

> y = array([1, 3, 5])

> y[1]
3

> y > 3
array[(False, False, True)]

> y[y > 3]
array[(5)]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Numpy dimensions of an 2-D array

A

df.shape

> y = array([1, 3, 5],
[4, 5, 6])
y.shape
(2, 3) # 2 rows, 3 cols

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Numpy Subsetting 2-D array

A

> 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])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Numpy mean

A

np.mean()

also subset with
np.mean(df[:, 0])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Numpy median

A

np.median()

also subset with
np.median(df[:, 0])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Numpy coefficient
Are two things related np.corrcoef(x, y) also subset with np.corrcoef(df[:, 0], df[:,1])
26
Numpy std
np.std(x) also subset with np.std(df[:, 0])
27
Numpy sum
np.sum(x) also subset with np.sum(df[:, 0])
28
Numpy join two different lists into a single array
np.columnstack((df_x, df_y))
29
Matplotlib Line Chart
plt. plot(x, y) | plt. show()
30
Matplotlib Scatter Plot
plt. scatter(x, y) | plt. show()
31
Matplotlib Histogram
plt. hist(x, bins = #) | plt. show()
32
Matplotlib Customize (x axis, y axis, title, ticks)
plt. xlabel('x') plt. ylabel('y') plt. title('title') plt. yticks([0,1,2,3,4]) plt. xticks([0,1,2,3], ['0', '1B', '2B', '3B']) # Reassign numbers on y -axis and change the name of y-axis ticks)
33
Dictionary
dict = {'k':v, 'k1':v1, 'k2',v2....} world = {'Nepal': 30.5, 'India': 1000, 'Bhutan' : 0.5}
34
Dictionary find all keys
dict.keys() > world = {'Nepal': 30.5, 'India': 1000, 'Bhutan' : 0.5} > print(world.keys()) Nepal, India, Bhutan
35
Dictionary add Key
dict['k'] = v > world = {'Nepal': 30.5, 'India': 1000, 'Bhutan' : 0.5} > world['China'] = 1050 > print(world) {'Nepal': 30.5, 'India': 1000, 'Bhutan' : 0.5, 'China' : 1050}
36
Dictionary delete key
del(dict['k']) > world = {'Nepal': 30.5, 'India': 1000, 'Bhutan' : 0.5} > del(world['Bhutan']) > world world = {'Nepal': 30.5, 'India': 1000}
37
Pandas dataframe
pd.DataFrame(dict) > world = {'Nepal': 30.5, 'India': 1000, 'Bhutan' : 0.5} > df = pd.DataFrame(world)
38
Pandas CSV
pd.read_csv('path/to/dataframe.csv', index_col = 0) index_col = 0 means that the pd will not index the df
39
Pandas select columns
df['colname']
40
Pandas select columns but keep in df
df[['colname']]
41
Pandas select two columns
df[['col1', 'col2']]
42
Pandas select rows
df[#:#]
43
Pandas Label Based Discovery
df.loc[['k']] >df.loc[['RU']] Country Capital Area RU Russsia Moscow 17.1
44
Pandas Label Discovery Multiple Rows
df.loc[['k1', 'k2', 'k3']] >df.loc[['RU', 'IN']] Country Capital Area RU Russsia Moscow 17.1 IN India Delhi 3.2
45
Pandas Label Discovery Multiple Rows and columns
df.loc[['k1', 'k2', 'k3'], ['col1', 'col2'] >df.loc[['RU', 'IN'], ['Country', 'Capital']] Country Capital RU Russsia Moscow IN India Delhi
46
Pandas Index Discovery
df.iloc[[#]] >df.iloc[[1]] Country Capital Area RU Russsia Moscow 17.1
47
Pandas Index Discovery Multiple Rows
df.iloc[[#, #, #]] >df.loc[[1, 2]] Country Capital Area RU Russsia Moscow 17.1 IN India Delhi 3.2
48
Pandas Index Discovery Multiple Rows and Columns
df.iloc[[#, #, #], [#, #] >df.loc[[1, 2], [0, 1]] Country Capital RU Russsia Moscow IN India Delhi
49
Pandas [ ] vs [[ ]]
[ ] is a pd. series where as [[ ]] is a pd. dataframe
50
and
both booleans need to be true > False and False True > x = 12 > x > 7 and x < 15 True > False and True False
51
or
at least one boolean needs to be true >True or False True > x = 5 > x < 7 or x > 13
52
Numpy array equivalent of: and, or, not
logical_and() logical_or() logical_not() > y = [[5, 7, 9]] > np.logical_and(y > 5, y <9) [[False, True, False]]
53
Filtering (subset) pd dataframe
Filter > df2 = df[‘col’] > # or Subset > df2 = df[df[‘col’] > #]
54
Subset using NP
Filter > np.logical_and(df['col'] > #, df['col'] < #) or subset > df[np.logical_and(df['col'] > #, df['col'] < #)]
55
Enumerate FOR loop
> fam = [1.5, 1.6, 1.7] ``` > for index, height in enumerate(fam): > print(str(index) + ' : ' + str(height)) 1 : 1.5 2 : 1.6 3 : 1.7 ```
56
FOR loop over a dictionary
First always key and then value > world = {'Nepal': 30.5, 'India': 1000, 'Bhutan' : 0.5} > for k, v in world.items(): > print(k + ' : ' + str(v)) Nepal : 30.5 India : 1000 Bhutan : 0.5
57
FOR loop over rows
iterrows() not very efficient because on every iteration you are creating a new pandas series > for lab, row in brics.iterrows(): > print(lab + ' : ' + row['captial'] BR : Brasilia RU : Moscow
58
Calculate new column (Non math)
apply() > brics['name_length'] = brics['country'].apply(len) name_length BR Brazil Brasilia 6
59
Random number generator bw 1 - 0
np.random.rand()
60
Set random number manually
np.random.seed(#) sets the random seed, so that your results are reproducible between simulations. As an argument, it takes an integer of your choosing. If you call the function, no output will be generated. np.random.rand() if you don't specify any arguments, it generates a random float between zero and one. > np.random.seed(123) > coin np.random.rand(0, 2) #Randomly generate 1 or 0
61
Transpose and array
np.transpose(df)