I have downloaded a Kaggle Netflix csv (here). Then I tried to open it using the following code on both atom and visual studio:
import pandas as pd
datapath='~/Downloads/netflix_titles_nov_2019.csv'
netflixdata=pd.read_csv(datapath)
netflixdata.describe()
It runs without error but does not output anything. However, when I do it on the terminal it shows the .csv information.
Am I missing something? Like an extension, etc..
When running commands in terminal, things operate differently. If you are running your code in an IDE of sorts, you need to tell your code to display the output. If you just want to visually see it, this can be done as easy as just printing the output with print(netflixdata.describe())
tl;dr
If you are expecting output from a script, you should surround it with print.
Explanation
In Python, the way an object is displayed is governed by its __repr__ function (think "representation"). Here's an example:
class MyUncoolClass:
pass
class MyCoolClass:
def __repr__(self):
return "Check out this repr"
>>> MyUncoolClass()
<__main__.MyUncoolClass object at 0x1063c0048>
>>> MyCoolClass()
Check out this repr
When you call netflixdata.describe(), you get a data frame back:
>>> type(netflixdata.describe())
<class 'pandas.core.frame.DataFrame'>
What you are actually viewing when you call .describe() in the shell is the output of the dataframe's __repr__ function, which is a string. This string will be shown when you print it, or when it's displayed in the console. However, when you run a python script, unless you (or something else) explicitly calls print on some object, it will not be printed.
Related
I'll want to know how to call a function in vs code. I read the answer to similar questions, but they don't work:
def userInput(n):
return n*n
userInput(5)
And appends nothing
def Input(n):
return n*n
And in the terminal:
from file import *
from: can't read /var/mail/file
Can somebody help me?
You are doing everything correctly in the first picture. In order to call a function in python on vs code you first have to define the function, which you did by typing def userInput(n):. If you want to see the result of your function, you should not use return, you should use print instead. Return is a keyword- so when your computer reaches the return keyword it attempts to send that value from one point in your code to another. If you want to see the result of your code, typing print (n) would work better.
Your code should look like this:
def userInput(n):
print (n * n)
userInput(5)
The code would print the result 25
Your terminal is your general way to access your operating system, so you have to tell it that you want it to interpret your Python code first.
If you want to run the file you're typing in, you have to first know the location of that file. When you type ls in your terminal, does the name of your Python file show up? If not, hover over the tab in VSCode (it's close to the top of the editor) and see what path appears. Then in your terminal type cd (short for "change directory") and then the path that you saw, minus the <your filename here>.py bit. Type ls again, and you should see your Python file. Now you can type python <your filename here>.py to run it (provided you have Python installed).
You could also run the IDLE by just typing python in your terminal. This will allow you to write your code line-by-line and immediately evaluate it, but it's easier to write in VSCode and then run it with the method I described before.
I'm taking a Udemy course on Python (my first language) and the environment of choice is Jupyter. When I try to write that code in Sublime, I can't get the same output (there are no errors).
def splicer(mystring):
if len(mystring)%2 == 0:
return "Even"
else:
return "Odd"
names = ["Andy", "Eve", "Sally"]
list(map(splicer,names))
You need to print the result!
print(list(map(splicer,names)))
In Jupyter, it automatically prints the representation of a statement, where as when you're writing applications, you need to print if you want the result to be shown on the screen.
jupyter acts as a python interpreter, so if you enter an object it automatically prints the result underneath. Sublime is a text editor, so it is only executing the code you are giving it. It is running list(map(splicer,names)) but it is not displaying the object because you are not telling it to.
So the interpreter (jupyter) is executing your python code in real time and interpreting (printing to screen). The text editor is only executing your python code. Therefore, you need to add a print statement to your object to have the editor print the object to screen:
print(list(map(splicer,names)))
I wrote a program of two lines in python. At first I tested it in the python shell. Here it is:
>>>state=True
>>>type(state)
<class 'bool'>
The output was as I expected in the shell.
And then I wrote these instructions in a file named main.py.
#---------------main.py----------------#
state=True
type(state)
Then I executed this program using linux terminal as root user. The output was nothing
[manjaro ~]# python main.py
[manjaro ~]#
I expected that the output would be as it was in the shell. As a beginner In python I don't know why there was no output. Please help me to understand why there was no output.
What you see is the raw representation of the object which is returned by __repr__ method of the respective object. It's what is called when you type an object in Python's interactive shell. When you're in a file you need to print the result using print function that triggers the __str__ method.
state=True
print(type(state))
I know about the built-in function type()
It works fine in the interactive mode of python. But when you try and use it in a .py file, nothing gets displayed.
Which method should I use if I want to check the data type inside a python script/file?
It seems to me that the problem is not the type function, but rather how you are generating output. In interactive mode, stuff that you type on the console generates output, but when scripting it does not. You need to explicitly tell it to output:
print(type(x))
I want to make a function that can determine the source code of how it was called. I'm aware of how to do this generally with the inspect module. For example, this question, works well and provides my desired output in the lines variable as shown below:
def hello(x):
frame,filename,line_number,function_name,lines,index=\
inspect.getouterframes(inspect.currentframe())[1]
print(frame,filename,line_number,function_name,lines,index)
The problem is that this solution doesn't work in an interactive command line session. For example, from a command line, the result looks like:
>>> y = hello(7)
(<frame object at 0x01ECA9E8>, '<stdin>', 1, '<module>', None, None)
The problem is that the source file is '<stdin>', so the lines variable is None. How can I access the calling line to find the result containing the string y = hello(7) during an interactive session?
It may not be possible, as #abarnert says. There are at least partial workarounds, however.
Getting source code lines isn't the biggest problem; modules like readline keep track of them. Interactive Python and iPython expose their lines slightly differently (sigh), but that too can be equalized. (My show package, for example, does this; its linecacher module puts a veneer on to equalize source access for normal Python and the different interactive flavors.)
The bigger problem is that, even once you have the source code, inspect doesn't provide legitimate line numbers (e.g. inspect.currentframe().f_back.f_lineno works great in normal code, but gives values like 1 or the point of the call in <stdin> when called interactively.)
But I'm not quite ready to call it impossible. Based on tracebacks generated by interactive Python and iPython, it appears that there may be sufficient information available to reconstruct "where did this call come from?" How much effort that would take, and how robust the answers would be...those are open questions.