I have a dataframe that is 49x10, and I need to find the percent change for each index item to a specific column in that row, for each index item.
Here is a sample of the dataframe being 5x10.
-15 -10 -5 -2 -1 0 1 2 5 10 15
symbol
ADS 80.098282 77.709549 72.130524 69.178314 72.947937 74.548950 75.364349 77.907074 79.691963 73.998260 67.965523
BEPC 49.635834 48.256989 45.957493 45.627144 47.968739 49.610779 48.056187 49.659355 56.013733 56.412094 59.579567
CBSH 70.400002 69.800003 70.533333 66.476189 65.250000 66.910004 66.180000 68.809998 68.089996 68.839996 68.110001
CIG 2.014711 2.097131 2.225340 2.371864 2.280286 2.089544 2.070374 2.127884 2.127884 2.204564 2.377095
CPE 12.200000 13.100000 11.800000 11.100000 10.400000 10.690000 10.050000 10.040000 8.340000 6.920000 6.630000
Essentially, I for column 0, I am trying to find the percent change centered around that date. which I have done with:
for col in df.columns:
df[col] = (df[col] - df['0'])/df['0']*100
however, that returns a dataframe that looks like this:
-15 -10 -5 -2 -1 0 1 2 5 10 15
symbol
ADS 7.443876 4.239629 -3.244079 -7.204174 -2.147600 0.0 inf inf inf inf inf
BEPC 0.050503 -2.728823 -7.363896 -8.029777 -3.309846 0.0 inf inf inf inf inf
CBSH 5.215958 4.319234 5.415228 -0.648356 -2.480950 0.0 inf inf inf inf inf
CIG -3.581304 0.363103 6.498839 13.511108 9.128440 0.0 inf inf inf inf inf
CPE 14.125353 22.544442 10.383542 3.835368 -2.712815 0.0 inf inf inf inf inf
The script is modifying df['0']
but I'm not sure how to find a workaround for it. The expected outcome would be percent change from df['0']
, as what happened before it iterated through the 0 placement.
Any help would be appreciated.
CodePudding user response:
The answer of @nonDucor found the mistake but I would be cautious to mix percentages with absolute values. (All columns are in percentage except for column '0' which you left unchanged). To complete the answer you should update the percentages of the reference column at the end.
# Compute percentages for all non-reference columns
for col in df.columns.drop('0'):
df[col] = (df[col] - df['0'])/df['0']*100
# Update the reference columne
df['0'] = 0.0
CodePudding user response:
Since column '0' is your reference, you should not change it (because the variation of it against itself is always 0%):
for col in df.columns:
if col == '0':
continue # Skip the reference column
df[col] = (df[col] - df['0'])/df['0']*100