Home > database >  Python: different results on CMD, IDLE and jupyther notebook
Python: different results on CMD, IDLE and jupyther notebook

Time:12-30

I am new with Python. I can't understand why, working with a simple code, I can see a result on the jupyter notebook and on the IDLE (>>>) but not on the CMD by launching a .py file The code is:

     import pandas as pd
     df = pd.read_csv ("datasets / file.csv")
     df.head ()

Why launching the xyz.py file I can't see the result on the CMD, while I can see it on the notebook through the xyz.ipynb file or through the IDLE (>>>) ? The code is the same, the path is correct .

Thanks

CodePudding user response:

DataFrame.head(n) returns the first n rows of the dataframe. This however does not print it:

print(df.head())

Will print the head.

CodePudding user response:

Jupyter and IDLE execute python code in interactive mode, which doesn't require the print function. In contrast, running python xyz.py is not in interactive mode, so nothing shows up with how you have your code written.

 import pandas as pd
 df = pd.read_csv ("datasets / file.csv")
 print(df.head())

will print df.head() in both jupyter notebooks and when it is run as python xyz.py

CodePudding user response:

Interactive environments like IDLE are meant for easy interactive exploration, so they automatically print the value of each expression. If you want to check the value of some variable var, it's easier to just type var, than print(var).

But when you run a Python script non-interactively, you need to explicitly print what you want to print:

print(df.head())

Overall, if you're not typing some instructions on the spot, but writing a script for later use, you should always do that.

  • Related