This is how I did it :
df = pd.DataFrame({
'a' : [2.0,1.0,3.5,2.0,5.0,3.0,1.0,1.0],
'b' : [1.0,-1.0,3.5,3.0,4.0,2.0,3.0,2.0],
'c' : [2.0,2.0,2.0,2.0,-1.0,-1.0,-2.0,-2.0],
})
a b c
0 2.0 1.0 2.0
1 1.0 -1.0 2.0
2 3.5 3.5 2.0
3 2.0 3.0 2.0
4 5.0 4.0 -1.0
5 3.0 2.0 -1.0
6 1.0 3.0 -2.0
7 1.0 2.0 -2.0
def new_column (df, column_name, column_value):
df[column_name] = column_value
return df
x = [i+1 for i in range(len(df))]
df2 = new_column(df, "help", x)
The problem with your approach is that you use "column" before creating it ! that is why it said : "NameError: name 'column' is not defined"
a b c help
0 2.0 1.0 2.0 1
1 1.0 -1.0 -2.0 2
2 3.5 3.5 2.0 3
3 2.0 3.0 -2.0 4
4 5.0 4.0 -1.0 5
5 3.0 2.0 1.0 6
6 1.0 3.0 -2.0 7
7 1.0 2.0 2.0 8