Здесь и далее копипаст 10-ти постов из репозитория Pandas-cookbook. Сначала я ничего не запомнил после чтения оригинала в NbViewer... там был авторский изыск с плохим форматированием (часть строк была просто скрыта), потом я не смог найти более полезной подборки примеров. Потом я понял, что здесь собраны как раз такие профессиональные приемы, которые я просто обязан запомнить, как "Отче наш..." Так что далее будут посты, начинающиеся с цифр, это и есть копипаст из Pandas-cookbook
This tour will work a little better in interactive mode, so it'll be better if you get IPython notebook installed and running. You can start it from a terminal by running
In [ ]:
ipython notebook
First, we need to explain how to run cells. Try to run the cell below!
In [ ]:
import pandas as pd
print "Hi! This is a cell. Press the вЦґ button above to run it"
You can also run a cell with Ctrl+Enter or Shift+Enter. Experiment a bit with that.
One of the most useful things about IPython notebook is its tab completion.
Try this: click just after read_csv( in the cell below and press Shift+Tab (or Tab if you're using IPython 1.x) 4 times, slowly
Try this: click just after read_csv( in the cell below and press Shift+Tab (or Tab if you're using IPython 1.x) 4 times, slowly
In [ ]:
pd.read_csv(
After the first time, you should see this:
In [5]:
from IPython.display import Image
Image("./images/tab-once.png")
Out[5]:
After the second time:
In [6]:
Image("./images/tab-twice.png")
Out[6]:
After the fourth time, a big help box should pop up at the bottom of the screen, with the full documentation for the
read_csv
function:
In [13]:
Image("./images/tab-4-times.png", width="100%")
Out[13]:
I find this amazingly useful. I think of this as "the more confused I am, the more times I should press Shift+Tab". Nothing bad will happen if you tab complete 12 times.
Okay, let's try tab completion for function names!
Okay, let's try tab completion for function names!
In [ ]:
pd.r
You should see this:
In [14]:
Image("./images/function-completion.png", width="30%")
Out[14]:
Writing code¶
Writing code in the notebook is pretty normal.
In [1]:
def print_10_nums():
for i in range(10):
print i,
In [2]:
print_10_nums()
Saving¶
As of the latest stable version, the notebook autosaves. You should use the latest stable version. Really.
Magic functions¶
IPython has all kinds of magic functions. Here's an example of comparing
sum()
with a list comprehension to a generator comprehension using the %time
magic.
In [3]:
%time sum([x for x in range(100000)])
Out[3]:
In [4]:
%time sum(x for x in range(100000))
Out[4]:
The magics I use most are
%time
and %prun
for profiling. You can run %magic
to get a list of all of them, and %quickref
for a reference sheet.
In [5]:
%quickref
You can also do nutty things like run Perl code in the notebook with cell magics. This is especially cool for things like Cython code, where you can try out Cython really fast with the
%%cython
magic (you'll need to install it).
In [6]:
%%perl
$_ = "whoa, python!";
s/python/perl/;
print
That's it for now!
Посты чуть ниже также могут вас заинтересовать
Комментариев нет:
Отправить комментарий