1

I am exceedingly new to programming, so go easy on me. I use Visual Studio Code as my editor in which I am working on a few python files. When I run the code, say for instance a pandas.dataframe.head() function, it doesn't return anything in the terminal. But when I print the same return value, I am seeing the data from a csv file as expected.

  • Can anyone explain what is going on?
  • Is it the case that when a function is run, that the data is stored but not necessarily displayed?
  • If so, is print the only means of displaying the value of a function when debugging?

Tried googling answers, but don't have clarity yet.

import pandas as pd

df = pd.read_csv('sample.csv')
df.head()
# print(df.head())

Expect some data to be displayed in the terminal

Jdoesph
  • 13
  • 5
  • It's probably how __str__ vs __repr__ are implemented in pd. Google str vs repr in Python. – minterm May 29 '19 at 00:50
  • __str__ is called when print is called and __repr__ is called from just the terminal – minterm May 29 '19 at 00:54
  • you have to use `print()` to display value on screen. `return` doesn't mean `print`. `head()` doesn't print value on screen, it only return value which you can assign to variable - `x = df.head()` - or use in `print()` to display it - `print(df.head())` or `x = df.head()` `print(x)`. If you run code in Python Shell or Jupyter then it automatically use `print()` or print(repr( ))` to display result of every command - to make life easier. But in script we don't need all results on screen and we use `print()` to display only some results. – furas May 29 '19 at 01:10

3 Answers3

1

I believe you have learned using either Jupyter or a python console. VS Code is an IDE; it's basically a glorified text editor with features that help developers. You must be used to using python in the console where each line/command it automatically prints the results, whereas you are now likely creating a script and expect the same thing to happen. I don't believe `return has anything to do with what you're asking as it acts the same either way.

EDIT (as I found the actual documentation)

When in an interactive console python calls sys.displayhook after every execution. here's the actual documentation:

If value is not None, this function prints repr(value) to sys.stdout, and saves value in builtins._. If repr(value) is not encodable to sys.stdout.encoding with sys.stdout.errors error handler (which is probably 'strict'), encode it to sys.stdout.encoding with 'backslashreplace' error handler.

sys.displayhook is called on the result of evaluating an expression entered in an interactive Python session. The display of these values can be customized by assigning another one-argument function to sys.displayhook.

Here's my very basic explanation I hope I explain it well enough

In the python console each line/command's results are printed after execution (ie: when you hit enter). (For context, every function/operation implicitly returns None if nothing else is returned, therefore not printed)

When running a python script, nothing will display in the console unless explicitly printed (other cases are uncaught error tracebacks, logging, or writing to stdout, etc...)

So basically the line

df.head()

In a script performs the head function on df and returns the results but nothing happens to the results, unless you assign it to a variable or print it. It's the same as just writing:

"This will only print in a console"

If that line is executed in an interactive console it will call sys.displayhook with the value and print the results:

'This will only print in a console'

But if ran in a script it is essentially a needless line of code unless assigned to a variable.

Basically, the console assumes you want to see results as you code. (basically calling a special print at every line that doesn't print None and isn't called when print is explicitly run) Whereas when running a script it only prints to the console when explicitly asked or other special cases.

Jab
  • 26,853
  • 21
  • 75
  • 114
0

Are the first 5 rows of your 'sample.csv' file blank per chance? If not selected, the df.head() returns (https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.head.html) the first 5 rows. So:

import pandas as pd
df = pd.DataFrame({'animal':['alligator', 'bee', 'falcon', 'lion', 'monkey', 'parrot', 'shark', 'whale', 'zebra']})

print(df.head())

      animal
0  alligator
1        bee
2     falcon
3       lion
4     monkey
Mike
  • 201
  • 2
  • 14
0

If you want to get a value from a function and use it elsewhere, you should return that value at the end of the function definition. That way when you call the function at some point in your code and you can assign it to a variable which would store the output of the function

For instance

#test.py
def square(i):
   return i*i

def main():
   eight_square = square(8)
   print(eight_square)

Only if you print the output you can actually see it in the terminal when you run python3 test.py. There are other ways to check, what the value is in a variable, for instance using a debugger. Visual Studio can be configured with a debugger if it isn't set up. A breakpoint is to be set at the location where the value of the variable has to be found and the debugger has to be started.

Reference: Microsoft visual studio docs