I am attempting to use a for loop iterate through a list of column names and on each iteration create a new column based on a string prefix and the original column name (stored in the list). the problem is that the dataframe thinks the variable is a series in the frame when
cols_to_transform = ['Price','Km_From_CBD','Local_Median_Price', 'AreaSize']
for x in cols_to_transform:
df.x #This is where the problem
df[x] = df[x>1]
newcolname = ('T1_Pre_'+ x)
df.newcolname = df.x + 1 #and same problem here
(DataFrame' object has no attribute 'x')
(DataFrame' object has no attribute 'newcolname' )
Ideally I would have a "global variable" or parameter that overrides the expected pandas object and takes the contents of the variable as a column name rather than the variable itself.
I cannot use pandas apply as in addition to creating the columns I need to create a series of subplots showing the change in transformation.
I know I can cast the whole line of code to string and then use exec, but I'm getting really over doing this as it feels like a cheap work around.
Thanks in Advance! Also it's my first question, go easy on me :)