I’m trying to make a function that adds new columns with numbering:
JavaScript
x
6
1
x = [i+1 for i in range(len(df))]
2
3
def new_column (df, column):
4
df.sort_values(by=column, ascending=False)
5
df['new_col'] = x
6
But, when I call it, I get an eroor:
JavaScript
1
2
1
new_column(df, column_name)
2
NameError: name ‘column’ is not defined
What did I do wrong?
Advertisement
Answer
Use:
JavaScript
1
28
28
1
np.random.seed(2021)
2
3
df = pd.DataFrame({'a':np.random.randint(1,10, size=10)})
4
print (df)
5
6
7
def new_column (df, column):
8
#assign back or use inplace=True
9
df = df.sort_values(by=column, ascending=False)
10
#df.sort_values(by=column, ascending=False, inplace=True)
11
#add range
12
df['new_col'] = range(1, len(df) + 1)
13
#return ouput
14
return df
15
16
print (new_column(df, 'a'))
17
a new_col
18
5 9 1
19
3 7 2
20
6 7 3
21
7 7 4
22
8 7 5
23
9 7 6
24
1 6 7
25
4 6 8
26
0 5 9
27
2 1 10
28