3

I have the following code in a jupypter notebook:

# (1) How to add a new column?
test_csv['aggregator'] = None
print (test_csv)

# (2) How to remove a column?
test_csv.drop(columns=['platform'])

It prints the following:

enter image description here

Why is the second statement formatted tabularly (without a print statement) whereas the first one is just text data? Is there a way to force print-format the DataFrame with the nicely-formatted table applied?

Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
samuelbrody1249
  • 4,379
  • 1
  • 15
  • 58

4 Answers4

4

Ran Cohen has already mentioned the "why" part of the print function destroying the nice formatting.

To get a nicely-formatted(tabular format with grey and white colors) dataframe, you can leave it at the end of the cell, but this works only for a single dataframe. In case one wants to print multiple nicely-formatted dataframes one can use

1)

from IPython.display import display
display(df1) #displays nicely formatted dataframe1
display(df2) #displays nicely formatted dataframe2

OR

2)

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
df1
df2
#displays both dataframes, nicely formatted

Note: The nice formatting is Jupyter notebook rendering(stylyzing and displaying) the HTML version of the dataframe, the same way html code is rendered by browsers.(here though the true HTML code obtained from df1.to_html() of the table is less stylistic as standalone html code when rendered by browser as some extra style is added by Jupyter Notebook)

Source:

  1. Stackoverflow Article, same question,my answer attributed to the ones given there

  2. Found Solution Here First

  3. Stackoverflow Article, related to rendering

  • as of 2022 with jupyter lab , your first solution worked for me at least without needing to import display. Thanks! – Martin May 26 '22 at 12:59
2

test_csv.drop(columns=['platform']) does not actually drop the column. It just shows you the interim picture of dataframe by printing its state.

To actually drop the column:

test_csv.drop(columns=['platform'], inplace=True)

OR

test_csv.drop('platform', axis=1, inplace=True)

test_csv['aggregator'] = None changes the state of the dataframe by assigning a new column to it. Hence, it does not print anything.

Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
2

Why does jupyter sometimes print a DataFrame formatted and sometimes as text?

if you use the function - print it will print it as text because print is using the function to_string for any object it gets.

and when you "leave" the data frame at the and of the cell it will show it as a table because it one of the functions that Jupiter does.

enter image description here

the function test_csv.drop(columns=['platform']) returns df if you want it to do the drop in the dataFrame you have to use inplace=True or df=df.drop(col)... and than print the dataFrame

Ran Cohen
  • 721
  • 6
  • 15
  • 1
    I see. So left alone, the `df` will 'print' without a `print()` statement? – samuelbrody1249 Aug 15 '20 at 20:19
  • 1
    Yes - Kind of, it will print it out but without using the function print. I think its the only IDE that does it, what makes it popular when experiments and classrooms – Ran Cohen Aug 15 '20 at 20:23
  • 1
    thanks. When you say IDE, is that part of the `ipython` core, or the jupyter viewer? – samuelbrody1249 Aug 15 '20 at 20:25
  • 1
    I'm not sure about Ipython - because I'm not using it out of the jupyter notebook - So I'm talking about jupyter notebook vs pycharm, spyder, etc... – Ran Cohen Aug 15 '20 at 20:31
0

I think you are running both the statements in the same cell of the Jupyter Notebook

You can run the below snippet in one cell

# (1) How to add a new column?
test_csv['aggregator'] = None
test_csv # no need to use print

and in the other cell

# (2) How to remove a column?
test_csv.drop(columns=['platform'])
Rajesh
  • 766
  • 5
  • 17