Question: I have a dataframe like this  Name Year Value 0 Mexico 1961 14357 1 Mexico 1961 15161 2 Mexico 1961 514658 3 Mexico 1962 15559 4 United States of America 1977 2191197 5 United States of America 1978 2470734 6 United States of America 1978 52470734 7 United States of America 1979 2737377 8 United States of America 1979 52731457 9 United States of America 1980 3029030 10 United States of America 1980 53024589 11 United States of America 1981 3272565 12 United States of America 2010 15199150 13 United States of America 2010 515543018 14 United States of America 2011 15861873 15 United States of America 2011 16250364  I want to convert it to:  Name Year Value 0 Mexico 1961 14357 1 Mexico 1961 15161 2 Mexico 1961 14658 3 Mexico 1962 15559 4 United States of America 1977 2191197 5 United States of America 1978 2470734 6 United States of America 1978 2470734 7 United States of America 1979 2737377 8 United States of America 1979 2731457 9 United States of America 1980 3029030 10 United States of America 1980 3024589 11 United States of America 1981 3272565 12 United States of America 2010 15199150 13 United States of America 2010 15543018 14 United States of America 2011 15861873 15 United States of America 2011 16250364  As you can see when the last column was significantly larger than its neighbours, it it replaced by a different number which is just removal of 5 from its front. for example for Mexico, in 3rd row 514658 is replaced by 14658, firstly because 514658 is significantly(5x-10x) larger than its neighbours ie 15161 and 15559. Similarly for USA, United States of America,1979,52731457 is replaced by United States of America,1979,2731457 Similarly  United States of America,1978,52470734  United States of America,1980,53024589  United States of America,2010,515543018   are replaced by  United States of America,1978,2470734  United States of America,1980,3024589 and  United States of America,2010,15543018 respectively.   But mind you,  firstly the first column ie Name should exactly match, secondly the last column ie Value has to start with 5 and finally Value has to be significantly larger ie with mostly one digit more than neighbours to avoid risk of removing false positives.  By now you might have understood this is a data cleaning exercise where some $ symbols have been written as 5 and hence have to be fixed.

Answer: You probably want to compare the previous or the next value, as if you only compare to the next one you will not be able to fix any errors that happen to be the last entry for that country. So create a lag and lead column grouped by country using pandas.DataFrame.shift() and then create a Boolean mask of values to replace where:  The value starts with 5. The value is at least 5 times larger than the previous value or next value.  Of course if you have countries with only one entry then this will not replace any values for that country. df['lag'] = df.groupby('Name')['Value'].shift(1) df['lead'] = df.groupby('Name')['Value'].shift(-1) replace_val = ( df['Value'].astype(str).str.startswith('5') & ( (df['Value'] > df['lag'] * 5) | (df['Value'] > df['lead'] * 5) ) ) df.loc[replace_val, 'Value'] = df.loc[replace_val, 'Value'].astype(str).str[1:].astype(int) df.drop(["lag", "lead"], axis = 1, inplace=True)  Output:  Name Year Value 0 Mexico 1961 14357 1 Mexico 1961 15161 2 Mexico 1961 14658 3 Mexico 1962 15559 4 United States of America 1977 2191197 5 United States of America 1978 2470734 6 United States of America 1978 2470734 7 United States of America 1979 2737377 8 United States of America 1979 2731457 9 United States of America 1980 3029030 10 United States of America 1980 3024589 11 United States of America 1981 3272565 12 United States of America 2010 15199150 13 United States of America 2010 15543018 14 United States of America 2011 15861873 15 United States of America 2011 16250364  Data data = { "Name": [ "Mexico", "Mexico", "Mexico", "Mexico", "United States of America", "United States of America", "United States of America", "United States of America", "United States of America", "United States of America", "United States of America", "United States of America", "United States of America", "United States of America", "United States of America", "United States of America" ], "Year": [ 1961, 1961, 1961, 1962, 1977, 1978, 1978, 1979, 1979, 1980, 1980, 1981, 2010, 2010, 2011, 2011 ], "Value": [ 14357, 15161, 514658, 15559, 2191197, 2470734, 52470734, 2737377, 52731457, 3029030, 53024589, 3272565, 15199150, 515543018, 15861873, 16250364 ] } df = pd.DataFrame(data)