Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use variable in for loop as dataframe column name [duplicate]

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 :)

like image 655
Dominic Avatar asked Oct 16 '25 04:10

Dominic


1 Answers

Try this code:

cols_to_transform = ['Price','Km_From_CBD','Local_Median_Price', 'AreaSize']

for x in cols_to_transform:
    df[x]

    newcolname = ('T1_Pre_'+ x)
    df.newcolname = df[x] + 1 
like image 98
Ernest S Kirubakaran Avatar answered Oct 17 '25 16:10

Ernest S Kirubakaran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!