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
Q

Numpy coefficient

A

Are two things related
np.corrcoef(x, y)

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

26
Q

Numpy std

A

np.std(x)

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

27
Q

Numpy sum

A

np.sum(x)

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

28
Q

Numpy join two different lists into a single array

A

np.columnstack((df_x, df_y))

29
Q

Matplotlib Line Chart

A

plt. plot(x, y)

plt. show()

30
Q

Matplotlib Scatter Plot

A

plt. scatter(x, y)

plt. show()

31
Q

Matplotlib Histogram

A

plt. hist(x, bins = #)

plt. show()

32
Q

Matplotlib Customize (x axis, y axis, title, ticks)

A

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
Q

Dictionary

A

dict = {‘k’:v, ‘k1’:v1, ‘k2’,v2….}

world = {‘Nepal’: 30.5, ‘India’: 1000, ‘Bhutan’ : 0.5}

34
Q

Dictionary find all keys

A

dict.keys()

> world = {‘Nepal’: 30.5, ‘India’: 1000, ‘Bhutan’ : 0.5}
print(world.keys())

Nepal, India, Bhutan

35
Q

Dictionary add Key

A

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
Q

Dictionary delete key

A

del(dict[‘k’])

> world = {‘Nepal’: 30.5, ‘India’: 1000, ‘Bhutan’ : 0.5}
del(world[‘Bhutan’])
world
world = {‘Nepal’: 30.5, ‘India’: 1000}

37
Q

Pandas dataframe

A

pd.DataFrame(dict)

> world = {‘Nepal’: 30.5, ‘India’: 1000, ‘Bhutan’ : 0.5}
df = pd.DataFrame(world)

38
Q

Pandas CSV

A

pd.read_csv(‘path/to/dataframe.csv’, index_col = 0)

index_col = 0 means that the pd will not index the df

39
Q

Pandas select columns

A

df[‘colname’]

40
Q

Pandas select columns but keep in df

A

df[[‘colname’]]

41
Q

Pandas select two columns

A

df[[‘col1’, ‘col2’]]

42
Q

Pandas select rows

A

df[#:#]

43
Q

Pandas Label Based Discovery

A

df.loc[[‘k’]]

> df.loc[[‘RU’]]
Country Capital Area
RU Russsia Moscow 17.1

44
Q

Pandas Label Discovery Multiple Rows

A

df.loc[[‘k1’, ‘k2’, ‘k3’]]

> df.loc[[‘RU’, ‘IN’]]
Country Capital Area
RU Russsia Moscow 17.1
IN India Delhi 3.2

45
Q

Pandas Label Discovery Multiple Rows and columns

A

df.loc[[‘k1’, ‘k2’, ‘k3’], [‘col1’, ‘col2’]

> df.loc[[‘RU’, ‘IN’], [‘Country’, ‘Capital’]]
Country Capital
RU Russsia Moscow
IN India Delhi

46
Q

Pandas Index Discovery

A

df.iloc[[#]]

> df.iloc[[1]]
Country Capital Area
RU Russsia Moscow 17.1

47
Q

Pandas Index Discovery Multiple Rows

A

df.iloc[[#, #, #]]

> df.loc[[1, 2]]
Country Capital Area
RU Russsia Moscow 17.1
IN India Delhi 3.2

48
Q

Pandas Index Discovery Multiple Rows and Columns

A

df.iloc[[#, #, #], [#, #]

> df.loc[[1, 2], [0, 1]]
Country Capital
RU Russsia Moscow
IN India Delhi

49
Q

Pandas [ ] vs [[ ]]

A

[ ] is a pd. series where as [[ ]] is a pd. dataframe

50
Q

and

A

both booleans need to be true

> False and False
True

> x = 12
x > 7 and x < 15
True

> False and True
False

51
Q

or

A

at least one boolean needs to be true

> True or False
True

> x = 5
x < 7 or x > 13

52
Q

Numpy array equivalent of: and, or, not

A

logical_and()
logical_or()
logical_not()

> y = [[5, 7, 9]]
np.logical_and(y > 5, y <9)
[[False, True, False]]

53
Q

Filtering (subset) pd dataframe

A

Filter
> df2 = df[‘col’] > #

or

Subset
> df2 = df[df[‘col’] > #]

54
Q

Subset using NP

A

Filter
> np.logical_and(df[‘col’] > #, df[‘col’] < #)

or

subset

> df[np.logical_and(df[‘col’] > #, df[‘col’] < #)]

55
Q

Enumerate FOR loop

A

> 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
Q

FOR loop over a dictionary

A

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
Q

FOR loop over rows

A

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
Q

Calculate new column (Non math)

A

apply()

> brics[‘name_length’] = brics[‘country’].apply(len)

                           name_length BR  Brazil  Brasilia          6
59
Q

Random number generator bw 1 - 0

A

np.random.rand()

60
Q

Set random number manually

A

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
Q

Transpose and array

A

np.transpose(df)