0
$\begingroup$

I am a newbie in DS world, right now i am working on some EDA practice, and run into an issue here.

Here is my code:


Convert some columns to numeric values

for column in cols:
    auto[column] = pd.to_numeric(auto[column] 
    return auto

auto = read_auto_data()

  1. error 1: File "", line 4 return auto ^ SyntaxError: 'return' outside function

I tried to change return's indent:


Convert some columns to numeric values

for column in cols:
    auto[column] = pd.to_numeric(auto[column])
return auto

auto = read_auto_data()

  1. error 2: TypeError Traceback (most recent call last) in () 1 ## Convert some columns to numeric values 2 for column in cols: ----> 3 auto[column] = pd.to_numeric(auto[column]) 4 return auto 5

TypeError: 'function' object is not subscriptable

Can anyone help me understand what went wrong here?

Thanks

$\endgroup$
7
  • $\begingroup$ Please use four spaces indenting to get code as-is. Your formatting is impossible to read, and in Python indenting is important. $\endgroup$ Sep 12, 2018 at 19:21
  • $\begingroup$ Also, are you trying to use a list comprehension? Or is this inside of some function definition? List comprehensions don't use the return statement. $\endgroup$ Sep 12, 2018 at 19:22
  • $\begingroup$ how can i show the indent here? when i copied my code, there are indents, but when i save, the indents are gone. Just now i also tried to edit, i checked out in the 'advanced help', seems like use '-' should mean indent 4, '*' should mean indent 8, i add these in my code, still not showing the indent. $\endgroup$
    – user633599
    Sep 12, 2018 at 20:21
  • $\begingroup$ If you indent four spaces, you get the code environment. All further indenting inside that will be preserved. Every line of code should start with four spaces. $\endgroup$ Sep 12, 2018 at 20:24
  • $\begingroup$ Yeah, finally the indents showing here. Can u take a look now? $\endgroup$
    – user633599
    Sep 12, 2018 at 20:29

1 Answer 1

1
$\begingroup$

Try something like this (the function also needs to know what cols is - perhaps pass that in as another parameter?):

def read_auto_data(auto):
    for column in cols:
        auto[column] = pd.to_numeric(auto[column])
    return auto

auto = read_auto_data()
$\endgroup$
3
  • $\begingroup$ Let me try, i will let u know if it works or not ASAP. $\endgroup$
    – user633599
    Sep 12, 2018 at 20:54
  • $\begingroup$ i just tried, it is true, when i put the function together, it works. I previously have defined the function already, but the indent might get messed up since there is a lot of code in between. thank you very much $\endgroup$
    – user633599
    Sep 12, 2018 at 22:08
  • $\begingroup$ You're very welcome! Glad I could be of help. $\endgroup$ Sep 12, 2018 at 23:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.