Udemy Code Review Flashcards

1
Q

num = 12 name= Sam

Print ‘My number is 12 and my name is Sam’ in two ways

A

print(‘My number is {} and my name is {}’.format(num, name)) print(‘My number is {one} and my name is {two}’.format(one=num, two=name))

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

d = {‘k1’:[1,2,3]}

Grab number 2

A

d[‘k1’][1]

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

What is a set?

set function

A

A collection of unique elements, eg. {1, 2, 3}

If put in multiples, will return only uniques. e.g. {1, 1, 2, 2, 3, 3} will return {1, 2, 3}

You can pass a list to a set function to get the unique elements:

set([1, 1, 2, 3, 4 ,4, 6, 6])

Returns: {1, 2, 3, 4, 6}

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

Add 5 to this set:

s = {1, 2, 3}

A

s.add(5)

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

split method

A

string.split(‘delimiter’)

It splits a string into a list based on the delimiter, which is space by default

e.g.

s = ‘hello my name is Sam’

s.split() returns: [‘hello’, ‘my’, ‘name’, ‘is’, ‘Sam’]

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

Tuple unpacking

x = [(1, 2), (3, 4), (5, 6)]

print 1, 3, 5

A

for a, b in x:

print(a)

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

numpy equivalent of range function?

A

np.arange(start, stop, increment)

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

Make an 1x3 array of zeros

Make a 5x5 array of zeros

A

pass array dimensions as a tuple

np. zeros(3)
np. zeros((5, 5))

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

linspace

A

np.linspace(start, stop, numberpoints)

Creates an evenly spaced sequence of numbers of desired length (numberpoints)

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

Create a 4x4 identity matrix

A

np.eye(4)

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

Create 1x5 array with random numbers from uniform distribution

5x5 array same

A

This returns random numbers between 0 and 1

np. random.rand(5)
np. random.rand(5, 5)

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

Create 2x3 array of random numbers from the normal distribution

A

np.random.randn(2, 3)

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

Create an array of 10 random integers from 1 to 99

A

low number is included but high number is not

np. random.randint(1, 100, 10)
np. random.randint(low, high, numberpoints)

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

How do you reshape a 1x25 array into a 5x5 array? Use array ‘arr’

A

arr.reshape(5, 5)

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

Get the maximum value of the array ‘arr’

Get the minimum value of the array ‘arr’

Get the location of the maximum value of the array ‘arr’

A

returns index value of max value

arr. max()
arr. min()
arr. argmax()

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

arr = np.arange(0, 11)

slice_of_arr = arr[0:6]

slice_of_arr[:] = 99

What are the elements of arr?

A

arr = ([99, 99, 99, 99, 99, 99, 6, 7, 8, 9, 10])

17
Q

Pandas is built upon

A

Numpy

18
Q

What is the difference between pandas series and numpy arrays?

A

Panda series can use labeled indexes

19
Q

Change my_data into a panda Series with labels as index

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

my_data = [10, 20, 30]

A

Can also pass it a numpy array

pd.Series(data=my_data, index=labels)

20
Q

What is the index when you turn a dictionary into a pandas series?

A

The index are the keys.

e.g.

d = {‘a’:10, ‘b’:20, ‘c’:30}

pd.Series(d)

returns:

a 10

b 20

c 30

21
Q

USA 1

Germany 2

Italy 5

Japan 4

This is a pandas series ser1. How do you pull out 1?

A

ser1[index]

ser1[‘USA’]

22
Q

How do you drop a column from a dataframe?

How do you drop a row from a dataframe?

A

inplace specifies whether to modify original df

df.drop(‘column1’, axis=1, inplace=True)

df.drop(‘row1’, axis=0, inplace=True)

23
Q

How do you do AND and OR conditions with pandas boolean series? e.g. You want the df where df[‘W’] > 0 and df[‘Y’] > 1

A

You cannot use regular AND or OR operators.

You need to use & or |

e.g.

df[(df[‘W’] > 0) & (df[‘Y’] > 1)]

24
Q

How do you reset the index in a dataframe?

A

inplace is optional. True means that it will change the original dataframe. Default is False

df.reset_index(inplace=True)

25
Q

How do you set a new index?

A

inplace = True if want to change original df

df.set_index(column1, inplace=True)

26
Q

drop missing values from a dataframe

A

axis is optional and default is 0 (rows)

df.dropna(axis=0)

27
Q

How do you fill in missing values in a dataframe?

A

df.fillna(value= )

28
Q

What is concatenation? How do you concatenate dataframes?

A

axis=0 concatenates vertically

Combining dataframes

pd.concat([df1, df2, df3], axis = 0)

29
Q

How do you merge dataframes together based on a common column?

A

similar logic as merging SQL tables together

pd.merge(df1, df2, how=’inner’, on=’commoncolumn’)

30
Q

How do you combine dataframes based on an index?

A

df1.join(df2)

31
Q

How do you apply a function to a column in a dataframe?

A

dfd[‘col1’].apply(functionname)

32
Q

How do you drop a column from a dataframe?

A

df.drop(‘col1’, axis=1, inplace=True)

33
Q

Get a list of the column names of a dataframe

A

df.columns

34
Q

Sort a dataframe by a column

A

df.sort_values(by=’col1’)

35
Q

create a pivot table with multiple indexes

A

df.pivot_table(values=’col1’, index=[‘col2’, ‘col3’], columns=[‘col4’])

36
Q

Figure out working directory

A

pwd

37
Q

Save a dataframe to a csv

A

If don’t include index=False, the index is converted into a column

df.to_csv(‘My_output’, index = False)