Home > Enterprise >  Replace the value in a row if his value his greater than in the previous row in my DataFrame (Pandas
Replace the value in a row if his value his greater than in the previous row in my DataFrame (Pandas

Time:10-19

This is my problem, which may be so simple, but I am a novice. I have a DataFrame, and, in a determinate column, I want to replace a value in a row if this is greater than the previous row.

The steps I am following are:

  1. df1 = pd.read_csv("M10_10.txt") (Reading my CSV)
  2. In that CSV, there is a column named m_Crit200. That is the column which values I want to replace if they match my condition.
  3. I am using this, but it does not working:
for i in range(1,len(df6)):
    if df6.m_Crit200[i] < df6.m_Crit200[i 1]:
        df6.m_Crit200[i]=df6.m_Crit200[i 1]
    else:
        df6.m_Crit200[i]=df6.m_Crit200[i]

This is the "if" code I am using, but does not working. Sorry about my explanation, as I said, I am novice and this is my first time here.

Thanks in advance

This is an example of what I want: I have this

    Value
0   10
1   7
2   6
3   12
4   3
5   2
6   1

I want this

    Value
0   10
1   7
2   6
3   6
4   3
5   2
6   1 

I want to replace the value by the value at the previous row, is that is greater.

This is my second error. When I use the methods in the answers below, I get this

0     6.540991
1     6.540991
2     6.971319
3     6.971319
4     6.971319
5     6.971319
6     7.057385
7     6.540991
8     6.540991
9     6.282794
10    6.282794
11    6.540991
12    6.540991
13    7.315582
14    8.176239
15    8.090173
16    7.831976
17    5.594269
18    3.959021
19    3.528693
20    3.528693
21    3.528693
22    3.528693
23    3.528693
24    3.700824
25    3.614758
26    3.614758
27    3.356561
28    3.356561
29    2.926233
30    2.754101
31    2.754101
32    2.754101
33    2.840167
34    2.323773
35    2.323773
36    2.495904
37    2.495904
38    2.323773
39    1.463116
40    1.032788
41    1.032788
Name: m_Crit200, dtype: float64

And here I let you my original

0     6.540991
1     6.971319
2     6.971319
3     6.971319
4     6.971319
5     7.057385
6     7.057385
7     6.540991
8     6.627057
9     6.282794
10    6.713122
11    6.540991
12    7.315582
13    8.348371
14    8.176239
15    8.090173
16    7.831976
17    5.594269
18    3.959021
19    3.528693
20    3.528693
21    3.786890
22    3.528693
23    3.700824
24    3.700824
25    3.614758
26    3.872955
27    3.356561
28    3.356561
29    2.926233
30    2.754101
31    2.754101
32    2.840167
33    3.098364
34    2.323773
35    2.754101
36    2.495904
37    2.495904
38    2.323773
39    1.463116
40    1.032788
41    1.032788
Name: m_Crit200, dtype: float64

CodePudding user response:

You can use shift for this:

df.Value.where(df.Value.shift(periods=1).fillna(np.inf)>df.Value, df.Value.shift(periods=1))

#output
0    10
1     7
2     6
3     6
4     3
5     2
6     1

CodePudding user response:

Since you want a non-increasing Series, you can use an expanding window and choose the minimum value for each window:

df6["m_Crit200"] = df6["m_Crit200"].expanding().apply(min)

>>> df6
0     6.540991
1     6.540991
2     6.540991
3     6.540991
4     6.540991
5     6.540991
6     6.540991
7     6.540991
8     6.540991
9     6.282794
10    6.282794
11    6.282794
12    6.282794
13    6.282794
14    6.282794
15    6.282794
16    6.282794
17    5.594269
18    3.959021
19    3.528693
20    3.528693
21    3.528693
22    3.528693
23    3.528693
24    3.528693
25    3.528693
26    3.528693
27    3.356561
28    3.356561
29    2.926233
30    2.754101
31    2.754101
32    2.754101
33    2.754101
34    2.323773
35    2.323773
36    2.323773
37    2.323773
38    2.323773
39    1.463116
40    1.032788
41    1.032788
Name: m_Crit200, dtype: float64
  • Related