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

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

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 show PIL Image in ipython notebook

Updated 2021/11/17 When using PIL/Pillow, Jupyter Notebooks now have a display built-in that will show the image directly, with no extra fuss. display(pil_im) Jupyter will also show the image if it is simply the last line in a cell (this has changed since the original post). Thanks to answers from @Dean and @Prabhat for pointing … Read more

How to hide code from cells in ipython notebook visualized with nbviewer?

from IPython.display import HTML HTML(”'<script> code_show=true; function code_toggle() { if (code_show){ $(‘div.input’).hide(); } else { $(‘div.input’).show(); } code_show = !code_show } $( document ).ready(code_toggle); </script> <form action=”javascript:code_toggle()”><input type=”submit” value=”Click here to toggle on/off the raw code.”></form>”’)

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)