Output format without print function
If I run a JupyterNotebook cell with
dataframe.describe()
a pretty fromatted table will be printed like that: VSCode JupyterNotebook dataframe.describe() solo cell printing format
Output format with print function
If I run a cell with more than just one line code dataframe.describe() would not print anythink. Therefore I need to call
print(dataframe.describe()).
This leads to a totally different formatting though: VSCode JupyterNotebook printing dataframe.describe() with print function
Is there a way to print dataframe.describe() in the first format?
CodePudding user response:
There are multiple things to say here:
Jupyter Notebooks can only print out one object at a time when simply calling it by name (e.g.
dataframe
). If you want, you can use one cell per command to get the right format.If you use the function
print
, it will print anything as text because print is using the functionto_string
for any object it gets. This ispython
logic - in contrast, option 1) is Jupyter-specific...If you don't want to use a seperate cell and still get the right formatting, there are several options, one might be this:
from IPython.display import display display(dataframe)
CodePudding user response:
I assume you are using the same jupyter notebook files in both environments. If that is the case, the problem you are facing is related to the execution order of the steps defined in the notebook itself, as the output of the code cell itself will be the one of the execution of the last line of the cell.
Let me illustrate you with an example.
Having the defined the following a dataframe in pandas:
data = {'id':[1,2,3,4],'nome':['Paolo','Pietro','Giovanni','Maria'],'spesa':[23.4,34.5,99.2,50.1]}
The output of the following cell would be different between this two cases:
# Outputs the dataframe itself
dataframe1 = pd.DataFrame(data)
dataframe1.describe()
dataframe1
# Outputs the describe() function return value
dataframe1 = pd.DataFrame(data)
dataframe1
dataframe.describe()
Both cells execute the same two lines on the dataframe without changing its internal state, however, only the last line will be written to the cell output.
Best regards,
Iker