Display all the data column in Jupyter Notebook

During our data exploration, there are often times that too many data columns in the dataframe. By default, the Jupyter Notebook only display a handful of it for simplicity.

Here is the couple of ways to display all the columns:

import pandas as pd

from IPython.display import display

data = pd.read_csv(‘mydave.cvs’)

# Direclty set the options

pd.options.display.max_columns = None

display(data)

Or, you set_option method from pandas.

pd.set_option(‘display.max_columns’, None)

display(data)

To locally change the setting for an only specific cell, do the following:

with pd.option_context(‘display.max_columns’,None):

           display(data)

You can also do:

from IPython.display import HTML

HTML(data.to_html())

Using R in Jupyter Notebook

R has started to gain momentum in data science due to its easy-to-use and full of statistic packages. For longtime Python user, I want to run some R commands within Jupyter for pratical reasons, like some collaborators are using R for some tasks or just convenience. This article will show you how to do it.

  • Setup environment

Install R essentials in your current environment:

conda install -c r r-essentials

These ‘essentials’ include the packages dplyr, shiny, ggplot2, tidyr, caret and nnet. 

You can also create a new environment just for the R essentials:

conda create -n my-r-env -c r r-essentials

Now you’re all set to work with R in Jupyter.

How about install new packages in R for my usage in Jupyter?

There are two ways of doing it: 1. build a Conda R package by running:

conda skeleton cran xxx conda build r-xxx/

Or you can install the package from inside of R via install.packages() or devtools::install_github. But with one change: change the destination to conda R library.

install.packages(“xxx”,”home/user/anaconda3/lib/R/library)

  • Into good hands

THe interactivity comes mainly from the so-called “magic commands” which allows you to switch from Python to command line instructions (like ls, cat etc) or to write code in other languages such as R, Scala, Julia, …

After open Jupiter notebook, you should be able to see R in the console:

1

To switch from Python to R, first download the following pacakge:

%load_ext rpy2.ipython

After that, start to use R with the %R magic command.

# Hide warnings if there are any
import warnings
warnings.filterwarnings(‘ignore’)
# Load in the r magic
%load_ext rpy2.ipython
# We need ggplot2
%R require(ggplot2)
# Load in the pandas library
import pandas as pd
# Make a pandas DataFrame
df = pd.DataFrame({‘Alphabet’: [‘a’, ‘b’, ‘c’, ‘d’,’e’, ‘f’, ‘g’, ‘h’,’i’],
‘A’: [4, 3, 5, 2, 1, 7, 7, 5, 9],
‘B’: [0, 4, 3, 6, 7, 10,11, 9, 13],
‘C’: [1, 2, 3, 1, 2, 3, 1, 2, 3]})
# Take the name of input variable df and assign it to an R variable of the same name
%%R -i df
# Plot the DataFrame df
ggplot(data=df) + geom_point(aes(x=A, y=B, color=C))

Python traps you should know

Like every language, there are some easy to overlook traps when writing Python programs. Some of the traps are hidden and can cause big problems or errors for your program. Here are some of the most common traps a good Python programmer should be aware:

    •  1. a mutable object used as the default parameter

Like all the other languages, Python provides default parameters for functions which are great for making thing easier. However, things can become unpleasant if you have put a mutable object in the function as the default value for a parameter. Let’s look at an example:

1

A surprise? ! The root cause is that everything is an object in Python, even function and default parameter is just an attribute of the function. Default parameter values are evaluated when the function definition is executed.

Another more obvious example:

2

How to fix it?

According to Python document: A way around this is to use None as the default, and explicitly test for it in the body of the function.

3

  • 2.  x += y vs x = x+y

Generally speaking, the two are equivalent. Let’s look at the example:

4

As we can see, when using +=, it returns the same id.  In the first example (53,54), x points to a new object while the latter one (55,56) it modifies its value at the current location.

  • 3. Majic parathesis ()

In Python, () can represent a tuple data structure which is immutable.

5

What if you only have one element in the tuple:

6

Majic, it becomes an integer instead of a tuple. The right thing to do is this:

7

  • 4. Generated element is a list of list

This is like a 2-D array. Or generating a list with mutable element in it. Sounds very easy:

8

By adding a value 10 into the first element in the list, we populated all the elements with the same value. Interesting, hmmm? That’s not what I want!!

The reason is still the same: mutable object within the list and they all point to the same object. The right syntax is:
9

As seen above, there are many traps while using Python and definitely you should be aware of it.