-1

It seems simple but I can’t seem to find an efficient way to solve this in Python 3: Is there is a loop I can use in my dataframe that takes every column after the current column (starting with the 1st column), and subtracts it from the current column, so that I can add that resulting column to a new dataframe?

This is what my data looks like: image of the Dataframe

This is what I have so far, but when running run_analysis my "result" equation is bringing up an error, and I do not know how to store the results in a new dataframe. I'm a beginner at all of this so any help would be much appreciated.

storage = [] #container that will store the results of the subtracted columns
def subtract (a,b): #function to call to do the column-wise subtractions
    return a-b

def run_analysis (frame, store):
    for first_col_index in range(len(frame)): #finding the first column to use
        temp=[] #temporary place to store the column-wise values from the analysis
        for sec_col_index in range(len(frame)): #finding the second column to subtract from the first column
            if (sec_col_index <= first_col_index): #if the column is below the current column or is equal to 
                                                   #the current column, then skip to next column
                continue
            else:
                result = [r for r in map(subtract, frame[sec_col_index], frame[first_col_index])]
            #if column above our current column, the subtract values in the column and keep the result in temp
                temp.append(result)
        store.append(temp) #save the complete analysis in the store
Python learner
  • 1,159
  • 1
  • 8
  • 20

1 Answers1

0

Something like this?

#dummy ddataframe
df = pd.DataFrame({'a':list(range(10)), 'b':list(range(10,20)), 'c':list(range(10))})
print(df)

output:

   a   b  c
0  0  10  0
1  1  11  1
2  2  12  2
3  3  13  3
4  4  14  4
5  5  15  5
6  6  16  6
7  7  17  7
8  8  18  8
9  9  19  9

Now iterate over pairs of columns and subtract them while assigning another column to the dataframe

for c1, c2 in zip(df.columns[:-1], df.columns[1:]):
    df[f'{c2}-{c1}'] = df[c2]-df[c1]
print(df)

output:

   a   b  c  b-a  c-b
0  0  10  0   10  -10
1  1  11  1   10  -10
2  2  12  2   10  -10
3  3  13  3   10  -10
4  4  14  4   10  -10
5  5  15  5   10  -10
6  6  16  6   10  -10
7  7  17  7   10  -10
8  8  18  8   10  -10
9  9  19  9   10  -10
alec_djinn
  • 10,104
  • 8
  • 46
  • 71