How to subtract value in one row of pandas Data Frame from value in another row without for loop?

kuba_pol

I have a python pandas Data Frame with 2 columns: X, which is an angle (181 evenly-spaced values between 0 and 360), and R1, which is an electrical resistance measured for a given angle. I need to add a column "R2", with the result of the following operation:

R2(X) = R1(X) - R1(180 - X).
Negative values of (180 - X) should map to (540 - X), so R1(180 - 182) == R1(358).

In practice, it means the "R2" column should be in the form (using row numbers):

R1[0] - R1[90]
R1[1] - R1[89]
R1[2] - R1[88]
...
R[90] - R[180]
R[91] - R[179]
R[92] - R[178]
...
R[180] - R[90]

My first guess was to to loop over rows' numbers with condition on which rows to take into account. After reading this question and also that one I realized this is not a preferred solution. df.apply() and df.shift() also doesn't seem to be a choice. Since none of the suggestions mentioned in the posts above seem to be straightforward in my case, I'd like to ask: is it possible to avoid "for" loop and what would be the best solution for this particular problem? Is it possible to select values from "R1" column based on the corresponding value from "X" column?

Mustafa Aydın

You can separate the column from the middle point; for each half, substract the reversed of it from itself, and at the end concatenate:

middle = int(np.ceil(len(df) / 2))

first_half  = df["R1"].iloc[:middle].to_numpy()
second_half = df["R1"].iloc[middle-1:].to_numpy()

df["R2"] = np.concatenate([first_half - first_half[::-1],
                           (second_half - second_half[::-1])[1:]])

Reason for using NumPy is because in pandas, arithmetic operations look at index, so subtracting the "reversed" Series from itself would be 0 due to index alignment (you can try df["R1"].iloc[:middle] - df["R1"].iloc[:middle][::-1] and see all zeros).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Subtract one row from another row in an R data.frame

Delete series value from row of a pandas data frame based on another data frame value

How do I assign values from one data frame to another based on row value using R?

How to subtract one row value from another and assign the value to a different row all in the same column, by group, for multiple columns in R

How to subtract rows of one pandas data frame from another?

Add/subtract data frame row from another data frame r

Subtract previous row value from the current row value in a Pandas column

Python Pandas subtract value of row from value of previous row

How to divide value from one row with another

Pandas, How to subtract first row value from all values in a Series?

How do I assign each row of a data frame to its respective value from another data frame?

pandas how to insert value from another row

How to select one value from row and update anotther row without a loop?

How to get the whole row from data frame having maximum value for every 7 records in the pandas data frame?

How do I loop through a row of data, pull value from specific cell in that row, find it in another row, then enter a value in the intersecting box

Remove row with null value from pandas data frame

How to find value in dataframe with column and row combination of another data frame?

How to prevent pandas from only assigning value from one df to column of another for only one row?

how to subtract a value from one column from a value from a previous row, different column in r

How to fill NaN with a value in same row of the data frame using Pandas

how do you concatenated column value for row in pandas data frame?

Pandas find max value in one column and display from another row

Random value for each row in pandas data Frame

Split pandas data frame columns by row value

Python Dataframe subtract value from one column from each list element of another column of a row

How do I subtract the previous row from the current row in a pandas dataframe and apply it to every row; without using a loop?

SQLite - how to subtract one value from another

How to replace value with Another Row value in SAS from Data Set

Check previous row value to copy data from one column to another