Python for Data Analysis Flashcards
Learn Pandas and related tools
IPython: How to trigger Tab completion for private members/variables?
Before hitting Tab, type the underscore “_”
How to introspect a variable in IPython?
Type a question mark before or after the name: eg myvar?
How to show source code for a function/method var in IPython?
Type ?? after the symbol
How to run an external Python script in IPython?
Use %run pathname_of_script.py
In which namespace %run executes the script?
In an “empty namespace” (no imports or variables defined)
How to execute code from the clipboard in IPython?
Use %paste or %cpaste (the latter allows to review pasted code).
How to get help on “magic” commands in IPython?
Type ? after the magic command (eg. %debug?)
How to setup matplotlib integration within IPython? In Jupyter?
In IPython, use the %matplotlib magic command, alone.
In Jupyter, add the inline option:
%matplotlib inline
In Python3, how to convert a Unicode string to its UTF-8 bytes representation? How to do the reverse?
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 to unpack while iterating in Python?
eg. for a sequence of tuples with three elements:
for a, b, c in sequence: # do something
What does range(a, b, c) return?
An iterator from the starting point a included, up to but not including the endpoint b, with a step of c.
How to unpack the first few elements of a sequence?
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 to count the number of occurrences of an item in a tuple or list?
sequence.count(item)
Slicing operator, what does it return?
seq[start:stop]
start is included, stop is excluded
How to get a sequence composed of every other element?
seq[::2]