Inline animations in Jupyter

notebook backend ‘Inline’ means that the plots are shown as png graphics. Those png images cannot be animated. While in principle one could build an animation by successively replacing the png images, this is probably undesired. A solution is to use the notebook backend, which is fully compatible with FuncAnimation as it renders the matplotlib … Read more

Jupyter notebook display two pandas tables side by side

I have ended up writing a function that can do this: [update: added titles based on suggestions (thnx @Antony_Hatchkins et al.)] from IPython.display import display_html from itertools import chain,cycle def display_side_by_side(*args,titles=cycle([”])): html_str=”” for df,title in zip(args, chain(titles,cycle([‘</br>’])) ): html_str+='<th style=”text-align:center”><td style=”vertical-align:top”>’ html_str+=f'<h2 style=”text-align: center;”>{title}</h2>’ html_str+=df.to_html().replace(‘table’,’table style=”display:inline”‘) html_str+='</td></th>’ display_html(html_str,raw=True) Example usage: df1 = pd.DataFrame(np.arange(12).reshape((3,4)),columns=[‘A’,’B’,’C’,’D’,]) df2 = … Read more

Link Spark with iPython Notebook

I have Jupyter installed, and indeed It is simpler than you think: Install anaconda for OSX. Install jupyter typing the next line in your terminal Click me for more info. ilovejobs@mymac:~$ conda install jupyter Update jupyter just in case. ilovejobs@mymac:~$ conda update jupyter Download Apache Spark and compile it, or download and uncompress Apache Spark … Read more

Calling pylab.savefig without display in ipython

This is a matplotlib question, and you can get around this by using a backend that doesn’t display to the user, e.g. ‘Agg’: import matplotlib matplotlib.use(‘Agg’) import matplotlib.pyplot as plt plt.plot([1,2,3]) plt.savefig(‘/tmp/test.png’) EDIT: If you don’t want to lose the ability to display plots, turn off Interactive Mode, and only call plt.show() when you are … Read more

How to read a .xlsx file using the pandas Library in iPython?

I usually create a dictionary containing a DataFrame for every sheet: xl_file = pd.ExcelFile(file_name) dfs = {sheet_name: xl_file.parse(sheet_name) for sheet_name in xl_file.sheet_names} Update: In pandas version 0.21.0+ you will get this behavior more cleanly by passing sheet_name=None to read_excel: dfs = pd.read_excel(file_name, sheet_name=None) In 0.20 and prior, this was sheetname rather than sheet_name (this is … Read more

Format certain floating dataframe columns into percentage in pandas

The accepted answer suggests to modify the raw data for presentation purposes, something you generally do not want. Imagine you need to make further analyses with these columns and you need the precision you lost with rounding. You can modify the formatting of individual columns in data frames, in your case: output = df.to_string(formatters={ ‘var1’: … Read more

ipython notebook clear cell output in code

You can use IPython.display.clear_output to clear the output of a cell. from IPython.display import clear_output for i in range(10): clear_output(wait=True) print(“Hello World!”) At the end of this loop you will only see one Hello World!. Without a code example it’s not easy to give you working code. Probably buffering the latest n events is a … Read more

How can I check if code is executed in the IPython notebook?

The following worked for my needs: get_ipython().__class__.__name__ It returns ‘TerminalInteractiveShell’ on a terminal IPython, ‘ZMQInteractiveShell’ on Jupyter (notebook AND qtconsole) and fails (NameError) on a regular Python interpreter. The method get_ipython() seems to be available in the global namespace by default when IPython is started. Wrapping it in a simple function: def is_notebook() -> bool: … Read more