A.Papa
A.Papa

Reputation: 486

Creating a new column name based on a loop variable and an additional string

I want to create percentage change column for each column that is a float in my dataframe and stored it in a newn column each time with the name of the initial column and the add on "_change"

I tried this but it does not seem to work any idea?

for col in df.columns:
        if df[col].dtypes == "float":
           df[ col&'_change'] = (df.col - df.groupby(['New_ID']).col.shift(1))/ df.col

for example if my column is df["Expenses"] I would like to save the percentage change in df["Expenses_change"] Edited for adding example data frame and output

df initially

Index   ID  Reporting_Date  Sales_Am    Exp_Am
     0   1   01/01/2016        1000      900
     1   1   02/01/2016        1050      950
     2   1   03/01/2016        1060      960
     3   2   01/01/2016        2000      1850
     4   2   02/01/2016        2500      2350
     4   2   03/01/2016        3000      2850

after the loop

Index   ID  Reporting_Date  Sales_Am  Sales_Am_chge  Exp_Am  Exp_Am_chge
0        1  01/01/2016         1000     Null          900      Null
1        1  02/01/2016         1050     5%            950      6%
2        1  03/01/2016         1060     1%            960      1%
3        2  01/01/2016         2000     Null          1850     Null
4        2  02/01/2016         2500     25%           2350     27%
4        2  03/01/2016         3000     20%           2850     21%

keep in mind that i have more than 2 columns on my dataframe.

Upvotes: 5

Views: 19628

Answers (3)

DanSan
DanSan

Reputation: 128

As it has been mentioned in other answers, just by changing & for + should do it. I was getting issues with using dots instead of square brackets so I changed them too.

This code has been tested in Python 3 and it works :)

for col in df.columns:
        if df[col].dtypes == "float":
               df[col+'_change'] = (df[col] - df.groupby(['repeat_present'])[col].shift(1))/ df[col]

Enjoy!

Upvotes: 3

jpp
jpp

Reputation: 164673

String concatenation is performed in python via the + operator.

So changing to col+'_change' will fix this issue for you.

You might find it helpful to read the relevant python documentation.

Upvotes: 6

Zach
Zach

Reputation: 482

Why are you using '&' instead of '+' in

df[ col&'_change']

?

Upvotes: 5

Related Questions