Apply mathematics operation im each entry of csv

Viewed 46

I am trying to apply some mathematics operation in each entry of csv rowwise.

This is my dataset(example):

00000  101000  102000  103000 #(these are headers)    
 1       2       3       4          
 2       3       4       5      
 3       4       5       6       
 2       4       6       8      
 1       2       3       5      

My dataset consists more than 560 columns. I want to apply following idea to my dataset and create a new dataset:

1. In each entry of csv(rowise) apply new_entry = current_entry + (previous_entry + next_entry)/2
2. skip this operation for first and last entry

So what I want is >> row first row 1 2 3 4 >> 2 becomes 2+ (1+3)/2 = 4 and 3 becomes 3 + (2 + 4)/2 = 6. I want to apply this idea to all entries except first and last columns.

My new dataset will be :

00000  101000  102000  103000    
 1       4       6       4          
 2       6       8       5      
 3       8       10       6       
 2       8       12       8      
 1       4       6.5       5

I did this in python:

# X is my dataset

for i in range(1, len(X) - 1):
    X.iloc[i, :] = (X.iloc[i - 1, :] + X.iloc[i + 1, :]) / 2
print(X.head())

Instead of adding old previous value, it adds new value to next value.

Can I get little assists? Thank you

1 Answers

You can use list splicing without any for loops. If you convert it to numpy arrays (via .to_numpy()) then this should work.

x[:,1:-1] += (x[:,0:-2]+x[:,2:])/2

Basically, I'm selecting the center columns with x[:,1:-1] (all but left and right) and adding to that the average of all the columns but the last two (x[:,0:-2])and all but the first two (x[:,2:]). Since numpy will do element-wise addition you get the result you were looking for.

Note, you might have to specify data type with dtype='float'in order for it to work as you cannot do operations between an int and a float

Related