jeetkamal
jeetkamal

Reputation: 409

defining function for data frame columns

Recently I started learning python. I want to define a function for columns in data frame. I want square of every value in column. Please check this code. It results in error in fifth line.

def new_fun(df,col_name='Open'):
    out = []
    col=df[col_name]
    for element in col:
    out[element].append(element**2)
    return out

Upvotes: 0

Views: 24

Answers (1)

meiwenhua
meiwenhua

Reputation: 86

Python is its use of indentation to highlight the blocks of code. All statements with the same distance to the right belong to the same block of code. If a block has to be more deeply nested, it is simply indented further to the right.

You need to more indented in 5th line.

def new_fun(df,col_name='Open'):
    out = []
    col=df[col_name]
    for element in col:
        out[element].append(element**2)
    return out

Upvotes: 1

Related Questions