Compare value previous\next value in df and correct it based on conditions

Viewed 53

I have a dataframe that looks like this:

**time**            **price**
1103                   5  
0010                   10
0100                   6
0201                   8
0015                   7
0400                   4
0900                   6
0800                   3
1000                   4
1140                   5
1400                   2
0030                   1
0112                   0

time = [1103,0010,0100,0201,0015,0400,0900,0800,1000,1140,1400,0030,0112]
price= [5,10,6,8,7,4,6,3,4,5,2,1,0]

Both the columns have dtype object.

I want to compare every time value to its previous and next value. If the value is greater than previous value and greater than next value (if the next value does not start with 00), I want the value to be replaced by previous value +100. So in our sample data the result will look like this:

**time**            **price**
1103                   5  
0010                   10
0100                   6
0201                   8
0015                   7
0400                   4
0500 (changed)         6
0800                   3
1000                   4
1140                   5
1400  (not changed)    2
0030                   1
0112                   0

Would appreciate some help with that, thanks.

2 Answers

You need to convert time to integers for easy modification:

df['time'] = df['time'].astype(int)

df.loc[df['time'].gt(df['time'].shift())      # greater than previous
       & df['time'].gt(df['time'].shift(-1))  # greater than next
       & df['time'].shift(-1).ge(100),        # next greater than 100
      'time'] = df['time'].shift(fill_value=0).add(100)

df['time'] = df['time'].astype(str).str.zfill(4)

Output:

    time price
0   1103     5
1   0010    10
2   0100     6
3   0201     8
4   0015     7
5   0400     4
6   0500     6
7   0800     3
8   1000     4
9   1140     5
10  1400     2
11  0030     1
12  0112     0

I have made this:

df = dfd.to_dict() #dfd =  the old dataframe
l = []
for k in df['time']:
    l.append(df['time'][k])
for i in range(len(l)):
    try:
        if l[i] > l[i-1] and l[i] > l[i+1]:
            l[i] = l[i-1] + 100
    except IndexError:
        pass
for i in range(len(l)):
    df['time'][i] = l[i]
dfd = pd.DataFrame(df)

The logic is to transform into dictionary, make the computations with easy classic python ways and then re-transform it to DataFrame

Related