IPython Notebook output cell is truncating contents of my list - python

I have a long list (about 4000 items) whose content is suppressed when I try to display it in an ipython notebook output cell. Maybe two-thirds is shown, but the end has a "...]", rather than all the contents of the list. How do I get ipython notebook to display the whole list instead of a cutoff version?

pd.options.display.max_rows = 4000
worked for me
See : http://pandas.pydata.org/pandas-docs/stable/options.html

I know its a pretty old thread, but still wanted to post my answer in the hope it helps someone.
You can change the number of max_seq_items shown by configuring the pandas options as follows:
import pandas as pd
pd.options.display.max_seq_items = 2000

This should work:
print(str(mylist))
Simple!

How to disable list truncation in IPython:
Create an IPython config file if you don't already have one:
ipython profile create
Edit the config file to include this line:
c.PlainTextFormatter.max_seq_length = 0
Restart your notebook instance.

The following line prints everything in your list in a readable manner.
[print(x) for x in lis]

A quick hack if you're using pandas is to do
from pandas import DataFrame
from IPython.display import HTML
HTML(DataFrame(myList).to_html())

For cases where the output of print(mylist) is something like [1, 1, 1, ..., 1, 1, 1] then [*mylist] will expand the items into rows where all items are visible.

Here's a way to display the whole list in the IPython output cell that doesn't require Pandas:
from IPython.display import HTML
x = range(4000)
HTML('<br />'.join(str(y) for y in x))
It is also pretty easy to add additional HTML elements and get a more elaborate display. Clicking to the left of the output cell will now shrink the contents and add a local scroll bar.

just use the print command instead of calling the list directly. Like print mylist . It would not truncate then.

Related

How can I configure a line chart using pivot_ui?

I would like to create a dynamic line chart in Voila. How can I manipulate the below code to show a standard line graph where the x axis equals column "a" and the y axis equals column "b"? Potentially the user can then dynamically update the output to make the y axis equal to column "c" by drag and drop etc.
from pivottablejs import pivot_ui
import pandas as pd
import IPython
df = pd.DataFrame(("a": [1,2,3], "b": [30,45,60],"c": [100,222,3444]))
display.display(df)
pivot_ui(df,outfile_path='pivottablejs.html',
rendererName="Line Chart",
cols= ["b","c"]
rows= ["a"],
aggregatorName="Sum"
)
display.display(IPython.display.HTML('pivottablejs.html"))
Thank you.
This should be a comment but I cannot post code with comments easily.
Please always test your code, preferably in a new notebook so it is fresh kerenl, before you post it.
Your dataframe assignment won't work. Should it be something like below?
That display code won't work in Jupyter notebook classic or JupyterLab presently.
Try something like this for assignment and display:
import pandas as pd
df = pd.DataFrame({"a": [1,2,3], "b": [30,45,60],"c": [100,222,3444]})
display(df)
That works in the classic notebook, JupyterLab, and Voila to make & display the dataframe.
Related to this is that is advisable to develop for Voila in JupyterLab. JupyterLab's rendering machinery is more modern and so closer to what Voila uses.
You can easily test renderings in launches from the Voila binder example page. Go there and click 'launch binder for the appropriate rendering. From JupyterLab you can select the Voila icon from the toolbar just above an open notebook and get the Voila rendering on the side.

How do i stop Vscode from truncating output of python dataframe column

I've tried to mention pandas setting and set display width to no avail. I just want vscode to show me the full output. Some other posts said there was a setting called 'Data Science'. I could not find that anywhere in vscode.
This was a .py file it was running some other code which contain s3 bucket URLs which is why I refrained from showing the original code.
Nevertheless I can emulate the same in this simpler code snippet on another file which creates a dataframe as so.
import pandas as pd
df_col1 = [12132432432423432,32423432432534523,34354354353454333,44353453453453454,53454353453453453]
df_col2 = ['test_url_thats_too_big_to_display_here' + str(i) for I in df_col1]
df = pd.DataFrame(list(zip(df_col1, df_col2)), columns = ['a', 'b'])
print(df)
The above code creates 2 columns one with an id number and the other with an id appended with the URL. Below is the output of the code.
There is no reason to make data casting or novel series conversions. You can change pandas' configurations.
Check the documentation here.
Here a solution:
pd.get_option("display.max_colwidth") # this is for your info only
50
pd.set_option("display.max_colwidth", None) # after this you can print any column length
# try it out:
s = pd.Series([["a" * 150]])
s
As per #wwii 's suggestion. using
print(df.to_string())
works perfectly
Here is a sample output of the code with last line change to the above

Looping through ipynb files in python

So if I have the same piece of code inside of 10 separate .ipynb files with different names and lets say that the code is as follows.
x = 1+1
so pretty simple stuff, but I want to change the variable x to y. Is their anyway using python to loop through each .ipynb file and do some sort of find and replace anywhere it sees x to change it or replace it with y? Or will I have to open each file up in Jupiter notebook and make the change manually?
I never tried this before, but the .ipynb files are simply JSONs. These pretty much function like nested dictionaries. Each cell is contained within the key 'cells', and then the 'cell_type' tells you if the cell is code. You then access the contents of the code cell (the code part) with the 'source' key.
In a notebook I am writing I can look for a particular piece of code like this:
import json
with open('UW_Demographics.ipynb') as f:
ff = json.load(f)
for cell in ff['cells']:
if cell['cell_type'] == 'code':
for elem in cell['source']:
if "pd.read_csv('UWdemographics.csv')" in elem:
print("OK")
You can iterate over your ipynb files, identify the code you want to change using the above, change it and save using json.dump in the normal way.

Python Jupyter: Shortcut to copy output of a cell

Please see attached screenshot:
In Jupyter Python: Is there a shortcut to copy the output of a cell to clipboard? (ie without having to manually select and ctrl-c?)
Alternatively is there a python function that instead of print would return its output directly in the clipboard for it to be pasted later?
You may use the following piece of code:
import pandas as pd
df = pd.DataFrame(['Copy me to clipboard'])
df.to_clipboard(index=False,header=False)
what i have done is ---file -->Print Preview , it opens the whole output in another tab than you can copy whatever you want

Dataframe head not shown in PyCharm

I have the following code in PyCharm
import pandas as pd
import numpy as np
import matplotlib as plt
df = pd.read_csv("c:/temp/datafile.txt", sep='\t')
df.head(10)
I get the following output:
Process finished with exit code 0
I am supposed to get the first ten rows of my datafile, but these do not appear in PyCharm.
I checked the Project interpreter and all settings seem to be alright there. The right packages are installed (numpy, pandas, matplotlib) under the right Python version.
What am I doing wrong? Thanks.
PyCharm is not Python Shell which automatically prints all results.
In PyCharm you have to use print() to display anything.
print(df.head(10))
The same is when you run script in other IDE or editor or directly python script.py
For printing all data
print(df)
By Default it will print top 5 records for head.
print(df.head())
If you need 10 rows then you can write this way
print(df.head(10))
I did File-Invalidate Caches/Restart Option Invalidate and after that I was able to get the head:

Categories

Resources