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.
Create a new column by assigning the output to the DataFrame with a new column name in between the [] . Operations are element-wise, no need to loop over rows. Use rename with a dictionary or function to rename row labels or column names.
You can use the assign() function to add a new column to the end of a pandas DataFrame: df = df. assign(col_name=[value1, value2, value3, ...])
Why are you using '&' instead of '+' in
df[ col&'_change']
?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With