Skip to content
Advertisement

Building a function to read CSV

I am new to Python and building a function to read CSV. I am trying to use the pandas.read_csv() inside my function,and while the code gets compiled-i dont see the dataset (I know its an overkill, but am trying to learn it using a trial and error method).

JavaScript

I expect that when i run CSV('abc.csv'), it should create a df in my variable explorer. Unfortunately, the function gets compiled, but nothing is there

JavaScript

Advertisement

Answer

The following example, taken from Read the Docs: Variables and Scope, illustrates the issue that you’re experiencing — dataset was created in your CSV function, but no longer exists outside of the scope of that function:

JavaScript

In this example, variable d is similar to your dataset variable — it’s trashed as soon as execution of the function is complete.

Instead:

JavaScript

will create a DataFrame variable df that you can view in variable explorer.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement