python-novice-gapminder icon indicating copy to clipboard operation
python-novice-gapminder copied to clipboard

Use display() not print() to display a dataframe in a jupyter notebook!

Open djinnome opened this issue 6 years ago • 16 comments

Hi folks,

I want to submit a pull request that removes all print() statements from the lesson, and at the very minimum, replaces them with display(). Jupyter notebooks by default display dataframes in an easy-to read html table, whereas the print function displays an ugly, hard-to-follow, poorly formatted ascii tab-separated output. If you want to view the value of the last line of a notebook cell, then you don't even need display(). Plus, once folks are familiar with from IPython.display import display, then it is logical to mention additional display outputs such as Image(), Latex() and SVG.

Please let me know what the probability of accepting this pull request would be before I go ahead and do the work.

Sincerely,

Jeremy

djinnome avatar Nov 01 '18 16:11 djinnome

Good idea - I always use display in this context. Olav

vahtras avatar Nov 01 '18 16:11 vahtras

Making this change would weld us pretty firmly to the notebook as a teaching interface - I still don't introduce it until after the first coffee break because I find the cognitive load of a new kind of editing on top of learning a language is too much for some learners.

gvwilson avatar Nov 01 '18 16:11 gvwilson

This particular lesson is already welded to the notebook as a teaching interface, isn't it?

vahtras avatar Nov 01 '18 16:11 vahtras

I think a section introducing display() would be a good idea. It would be an especially good idea in the pandas section. I agree - the default printing of a pandas table is pretty ugly.

I think wholesale elimination of the print function is a bad idea. Why? Display does not work (so far as I can tell) from the python command line. This seems like a big missed idea if a learner is going to go on to writing python programs that live and are executed in the shell.

ntmoore avatar Nov 01 '18 17:11 ntmoore

I think introducing display() is generally a good idea, due to its easier to read output. And a clear tabular output of a DataFrame makes it easier for the human mind to process the information. But I agree with @ntmoore and @gvwilson that it should not be introduced to early, because learners should first get familiar with the basic print() function. A good point for introducing display() could be section "Reading Tabular Data into DataFrames", because of two reasons:

  1. In this section of the lesson the pandas DataFrame is introduced and display helps a lot to easily understand the structure of a DataFrame in a visual way.
  2. If the display() function is introduced by importing it like from IPython.display import display, this would be a nice repetition of the concept of importing libraries/modules of the previous section "Libraries"

Tillsa avatar Nov 20 '18 12:11 Tillsa

I would like to link issue #397 which proposes switching to JupyterLab from Jupyter notebook. Using JupyterLab instead of Jupyter notebook might help those instructors such as @gvwilson who teach Python from a console first and then introduce Jupyter notebooks later. Process of launching the Jupyter lab server is the same as launching the Jupyter notebook server..

$ jupyter lab

...instead of the following.

$ jupyter notebook

JupyterLab server UI is similar in many respects to RStudio UI. In particular, JupyterLab includes an IPython console/terminal as part of the UI which would help reduce the cognitive load for learners when switching between console and notebooks.

davidrpugh avatar May 14 '19 07:05 davidrpugh

For now, I think the easiest way to address this issue is to add a small discussion of display() in episode 8, which works with dataframes. Display would be especially relevant here because print doesn't render dataframes nicely.

ntmoore avatar Jun 05 '19 01:06 ntmoore

+1

djinnome avatar Jun 05 '19 02:06 djinnome

+1

vahtras avatar Jun 05 '19 10:06 vahtras

Sounds fine.

On Wed, Jun 5, 2019 at 5:43 AM Olav Vahtras [email protected] wrote:

+1

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/swcarpentry/python-novice-gapminder/issues/342?email_source=notifications&email_token=ACDYZOXLMGLSOGLGGJRIYCDPY6KD7A5CNFSM4GA44KSKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODW7KGYY#issuecomment-499032931, or mute the thread https://github.com/notifications/unsubscribe-auth/ACDYZOUNGHNXLTMOTG3E5TDPY6KD7ANCNFSM4GA44KSA .

--


Nathan Moore Mississippi River and 44th Parallel


ntmoore avatar Jun 05 '19 11:06 ntmoore

Actually, the discussion of display() should be in episode 7, Reading Tabular Data into DataFrames

djinnome avatar Jun 05 '19 17:06 djinnome

Sure, it would be fine there as well.

On Wed, Jun 5, 2019 at 12:24 PM Jeremy Zucker [email protected] wrote:

Actually, the discussion should be in episode 7, Reading Tabular Data into DataFrames https://swcarpentry.github.io/python-novice-gapminder/07-reading-tabular/index.html

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/swcarpentry/python-novice-gapminder/issues/342?email_source=notifications&email_token=ACDYZOVJPSGATHPNFS2YSGDPY7ZGFA5CNFSM4GA44KSKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODXANUOY#issuecomment-499178043, or mute the thread https://github.com/notifications/unsubscribe-auth/ACDYZOQFHMZA45MTE4XXH6TPY7ZGFANCNFSM4GA44KSA .

--


Nathan Moore Mississippi River and 44th Parallel


ntmoore avatar Jun 05 '19 17:06 ntmoore

This solved my issue with having to separate cells in my notebook! Thanks so much

FinancialRADDeveloper avatar Sep 17 '19 11:09 FinancialRADDeveloper

What's the rationale of using display(df) when Jupyter Lab will pretty print the table when df is the last line in a cell? Wouldn't that be the cleanest approach?

Also, the only issue I can think of with replacing print() would occur when exporting .ipynb -> .py. Upon exporting, print() works out of the box but display() requires from IPython.core.display import display to function. But I don't think anyone is exporting these pandas notebooks... so it should be cool.

shervinsahba avatar Oct 02 '19 22:10 shervinsahba

not working sir,still open new tab

Talha-Naeem avatar Apr 09 '20 10:04 Talha-Naeem

%matplotlib inline it works, and this command tell by my Sir Anees Ahmed

Talha-Naeem avatar Apr 11 '20 17:04 Talha-Naeem