Jupyter notebook: DataFrame not printing as table - pandas

In my Jupyter notebook version 4.2.3, when I add a cell such as
products = pd.read_csv('products.csv')
products.head()
len(products)
where products is a DataFrame, I expect a table to be displayed when the cell is run, as I have seen in other notebooks. But in order to display the DataFrame as a table, I have to first import the display method from from IPython.display and use display(products.head()), which works. What am I missing?

I caught my mistake. The last line in the cell is the one that's output. If I remove len(products) then the DataFrame prints as a table.

Related

How to increase length of ouput table or dataframe in Jupyter Notebook?

I am working on the Jupyter notebook and have been facing issues in increasing the length of the output of the Jupyter Notebook. I can see the output as follows:
I tried increasing the default length of the columns in pandas with no success. Can you please help me with it?
If you were using the typical way to view a dataframe in Jupyter (see my puzzelment about your screenshot in my comments to your original post) it would be things like this:
adapted from answer to 'Pretty-print an entire Pandas Series / DataFrame'
with pd.option_context('display.max_rows', None, 'display.max_columns', None):
display(df)
(Note that will work with the text-based viewing, too. Note it uses print(df) in the answer to 'Pretty-print an entire Pandas Series / DataFrame'.
Adjust the 'display.max_colwidth' if you want the entire column text to show:
with pd.option_context('display.max_rows', None, 'display.max_columns', None,'display.max_colwidth', -1):
display(df)
(If you prefer text like you posted, replace display() with print()
Generally with the solutions above the view window in Jupyter will get scrollbars so you can navigate to view all still.
You can also set the number of rows to show to be lower to save space, see example here.
You may also be interested in Pandas dataframe hide index functionality? or Using python / Jupyter Notebook, how to prevent row numbers from printing?.
As pointed out here, setting some some global options is covered in the Pandas Documentation for top-level options.
For display() to work these days you don't need to do anything extra. But if your are using old Jupyter or it doesn't work then try adding towards the top of your notebook file and running the following as a cell first:
from IPython.display import display

The Jupyter do not show border lines and grey blocks

everybody. I learn NumPy and pandas with the Jupyter. When printing, it does not show borderlines and grey blocks. Example:
Should be:
How to solve it? Thank you very much, everybody.
The 'borderlines and grey blocks' view is a rendered HTML version of your dataframe.
You can either just use b as #Carlos Bergillos mentioned, or use
from IPython.display import display
display(b)
Please see other very similar questions:
Show DataFrame as table in iPython Notebook
Display DataFrame in Jupyter Notebook, Depending on a Condition
Display data in pandas dataframe

Holoviz panel will not print pandas dataframe row in Jupyter notebook

I'm trying to recreate the first panel.interact example in the Holoviz tutorial using a Pandas dataframe instead of a Dask dataframe. I get the slider, but the pandas dataframe row does not show.
See the original example at: http://holoviz.org/tutorial/Building_Panels.html
I've tried using Dask as in the Holoviz example. Dask rows print out just fine, but it demonstrates that panel seem to treat Dask dataframe rows differently for printing than Pandas dataframe rows. Here's my minimal code:
import pandas as pd
import panel
l1 = ['a','b','c','d','a','b']
l2 = [1,2,3,4,5,6]
df = pd.DataFrame({'cat':l1,'val':l2})
def select_row(rowno=0):
row = df.loc[rowno]
return row
panel.extension()
panel.extension('katex')
panel.interact(select_row, rowno=(0, 5))
I've included a line with the katex extension, because without it, I get a warning that it is needed. Without it, I don't even get the slider.
I can call the select_row(rowno=0) function separately in a Jupyter cell and get a nice printout of the row, so it appears the function is working as it should.
Any help in getting this to work would be most appreciated. Thanks.
Got a solution. With Pandas, loc[rowno:rowno] returns a pandas.core.frame.DataFrame object of length 1 which works fine with panel while loc[rowno] returns a pandas.core.series.Series object which does not work so well. Thus modifying the select_row() function like this makes it all work:
def select_row(rowno=0):
row = df.loc[rowno:rowno]
return row
Still not sure, however, why panel will print out the Dataframe object and not the Series object.
Note: if you use iloc, then you use add +1, i.e., df.iloc[rowno:rowno+1].

How to add plot commands to a figure in more than one cell, but display it only in the end?

I want to do the following in a Jupyter Notebook:
Create a pyplot.figure in a cell;
For each subsequent cells, calculate values and plot them to that same figure without displaying anything;
At the end, in another cell, display the figure with the result of every previous plot command.
Currently, while using %matplotlib notebook, the figure is always displayed after the same cell it's been created, and I don't even call plt.show().
This is not the behavior I desire. Instead I would like to postpone the display of the figure for the last cell only, but the figure of course should contain the results of the sequential plot commands called in the cells in between.
You can capture the content of a cell of a jupyter notebook using the magic command %%capture. You can also hide any output of a specific line by putting a ; at the end of it.
Showing the figure can be done by simply typing the variable in which the figure is stored, e.g. fig.
Combining those techniques gives you
import matplotlib.pyplot as plt
%matplotlib notebook
%%capture captured
fig, ax=plt.subplots()
ax.plot([1,2,3]);
fig # now show the figure
which is probably more understandable in the acutal notebook like this:
Also see How to overlay plots from different cells?

Can I request all Pandas columns display in an IPython notebook?

By default, the number of columns displayed by pandas commands is limited to display.max_columns. Is there something like df.showall() that can be used to override this on a per-command bases?
The simplest way I have found to do this is the following:
from IPython.core.display import HTML
HTML(df.to_html())
This will display the whole table in the IPython notebook output cell - all rows and columns. Scrollbars will appear for large tables.
To display all columns but no more than N rows, use:
HTML(df.head(N).to_html())
You can define a context manager:
from contextlib import contextmanager
#contextmanager
def temp_option(option, value):
old_value = pd.get_option(option)
pd.set_option(option, value)
yield
pd.set_option(option, old_value)
Then do what you want, something like
>>>with temp_option('display.max_rows', 200):
print(df)
I thought pandas already had this feature, but I couldn't find it.