Skip to content
Advertisement

Can’t create function, that adds new column in DataFrame

I’m trying to make a function that adds new columns with numbering:

x = [i+1 for i in range(len(df))]

def new_column (df, column):
  df.sort_values(by=column, ascending=False)
  df['new_col'] = x

But, when I call it, I get an eroor:

new_column(df, column_name)

NameError: name ‘column’ is not defined

What did I do wrong?

Advertisement

Answer

Use:

np.random.seed(2021)

df = pd.DataFrame({'a':np.random.randint(1,10, size=10)})
print (df)


def new_column (df, column):
    #assign back or use inplace=True
    df = df.sort_values(by=column, ascending=False)
    #df.sort_values(by=column, ascending=False, inplace=True)
    #add range
    df['new_col'] = range(1, len(df) + 1)
    #return ouput
    return df
  
print (new_column(df, 'a'))
   a  new_col
5  9        1
3  7        2
6  7        3
7  7        4
8  7        5
9  7        6
1  6        7
4  6        8
0  5        9
2  1       10

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