简体   繁体   中英

How to compare current excel cell to previous excel cell in same column

I am trying to write a quick function to compare the current cell in a column to the cell just above (before) it. The idea is to perform a different operation on data that is in the same column but different value.

if (df.loc() != df.loc[::-1] & df1.loc() != df1.loc[:-1]):
df = df.iloc()
df1 = df1.iloc()

My thinking was to compare the current location to the current location -1, and if they are the same then to proceed. Otherwise do another task when the cell value changes. I am doing this to two different data frames that have the same column name in each that I am trying to read through.

   Connector  Pin        Adj.  
0       F123    1       2 6 7  
1       F123    2   1 3 6 7 8  
2       F123    3   2 4 7 8 9  
3       F123    4  3 5 8 9 10  
4       F123    5      4 9 10  
5       F123    6       1 2 7  
6       F123    7   1 2 3 6 8  
7       F123    8   2 3 4 7 9  
8       F123    9  3 4 5 8 10  
9       F123   10       4 5 9  
10      C137    1         2 1  
11      C137    2         1 

After iterating down this table, when the Connector changes from F123 to C137 I want to clear all columns above the first C137.

Considering the below dataframe:

print(df)

 Connector  Pin        Adj.
0       F123    1       2 6 7
1       F123    2   1 3 6 7 8
2       F123    3   2 4 7 8 9
3       F123    4  3 5 8 9 10
4       F123    5      4 9 10
5       F123    6       1 2 7
6       F123    7   1 2 3 6 8
7       F123    8   2 3 4 7 9
8       F123    9  3 4 5 8 10
9       F123   10       4 5 9
10      C137    1         2 1
11      C137    2           1

If you use:

df.drop(range(df.Connector.ne(df.Connector.shift()).cumsum().idxmax()))

   Connector  Pin Adj.
10      C137    1  2 1
11      C137    2    1

This will identify the change and drop those rows before where the change in connector has happen

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM