[SOLVED] Why does this function return a tuple?

Issue

I have defined a fairly simple function that is having an unexpected output. I’m using Steam game reviews and want to use a function to narrow the scope based on game title. This works fine:

one_game = games[games['title'] == "Robocraft"]

The output is a dataframe just like the original. However, when I try to make a function (by passing the same game name as an argument) to slice the dataframe as follows:

def slice(game):
    out = games[games['title'] == game],
    return(out)

I get a tuple that is "[362 rows x 5 columns],)" instead of a dataframe. Is that because of the return command or is that just something that happens when you use a user defined function?

This seems like all I would need to do is convert the tuple back to a dataframe. However, I can’t even do that! When I do, I get this error:

"ValueError: Must pass 2-d input. shape=(1, 362, 5)"

How can I get a dataframe as my output?

Thank you!

Solution

The comma at the end of the first line of your function is the problem. It wraps the preceding value in a tuple, see:

>>> 1
1

>>> 1,
(1,)

>>> a = 1
>>> type(a)
1

>>>> a = 1,
>>> type(a)
tuple

So just remove that comma, (and the parentheses after return, because return is a keyword, not a function):

def slice(game):
    out = games[games['title'] == game]
    return out

Answered By – richardec

Answer Checked By – Senaida (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.