[Tutor] Getting started with Pandas

Alan Gauld alan.gauld at yahoo.co.uk
Wed Aug 22 06:15:42 EDT 2018


On 22/08/18 07:46, Rafael Knuth wrote:
> import pandas as pd
> cities_lst = pd.read_table("cool_cities.csv")
> cities_lst.head()
> 
> I was trying to rewrite the above as a function.
> Unlike my code above, my function below did not return the first 5
> rows, but just nothing:
> 
> def cities(file_name):
>     import pandas as pd
>     cities_lst = pd.read_table(file_name)
>     cities_lst.head()
> 
> cities("cool_cities.csv")
> 
> Why does my function not return the first 5 rows?
> What's the mistake I am making here?

You are not returning anything.
You need to use the return keyword otherwise your
function just generates the data internally then
throws it away again.

def double(x):
    value = x * 2  # no return statement

def treble(x):
    value = x * 3
    return value

print(double(4))   -> None
print(treble(4))   -> 12


HTH

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list