Python for Data Analysis Flashcards

Learn Pandas and related tools

1
Q

IPython: How to trigger Tab completion for private members/variables?

A

Before hitting Tab, type the underscore “_”

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

How to introspect a variable in IPython?

A

Type a question mark before or after the name: eg myvar?

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

How to show source code for a function/method var in IPython?

A

Type ?? after the symbol

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

How to run an external Python script in IPython?

A

Use %run pathname_of_script.py

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

In which namespace %run executes the script?

A

In an “empty namespace” (no imports or variables defined)

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

How to execute code from the clipboard in IPython?

A

Use %paste or %cpaste (the latter allows to review pasted code).

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

How to get help on “magic” commands in IPython?

A

Type ? after the magic command (eg. %debug?)

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

How to setup matplotlib integration within IPython? In Jupyter?

A

In IPython, use the %matplotlib magic command, alone.

In Jupyter, add the inline option:

%matplotlib inline

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

In Python3, how to convert a Unicode string to its UTF-8 bytes representation? How to do the reverse?

A

Use the .encode(‘utf-8’) method:

mystring = “español”
mystring.encode(‘utf-8’)
b’espa\xc3\xb1ol’

To do the reverse operation, use .decode():

b’espa\xc3\xb1ol’.decode(‘utf8’)
“español”

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

How to unpack while iterating in Python?

A

eg. for a sequence of tuples with three elements:

for a, b, c in sequence:
    # do something
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does range(a, b, c) return?

A

An iterator from the starting point a included, up to but not including the endpoint b, with a step of c.

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

How to unpack the first few elements of a sequence?

A

Use the *rest syntax:

values = (1, 2, 3, 4, 5, 6)
a, b, *rest = values

rest
[3, 4, 5]

or, when not interested in the actual rest contents:

a, b, *_ = values

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

How to count the number of occurrences of an item in a tuple or list?

A

sequence.count(item)

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

Slicing operator, what does it return?

A

seq[start:stop]

start is included, stop is excluded

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

How to get a sequence composed of every other element?

A

seq[::2]

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

How to get the reverse of a sequence?

A

seq[::-1]

17
Q

How to iterate keeping track of the index?

A
for i, value in enumerate(sequence):
    # do something
18
Q

What does “zip” do?

A

Pairs two sequences:

s1 = ['one', 'two', 'three']
s2 = [1, 2, 3]

zip(s1, s2)
[(‘one’, 1), (‘two’, 2), (‘three’, 3)]

Can be used to iterate simultaneously over multiple collections.

Can be used to unzip a zipped sequence:

pairs = [(‘Alessandro’, ‘Manzoni’), (‘William’, ‘Shakespear’), (‘Giacomo’, ‘Leopardi’)]

first, last = zip(*pairs)

19
Q

How to unzip a “zipped” sequence?

A

pairs = [(‘Alessandro’, ‘Manzoni’), (‘William’, ‘Shakespear’), (‘Giacomo’, ‘Leopardi’)]

first, last = zip(*pairs)