简体   繁体   English

如果他的值大于我的 DataFrame (Pandas) 中的前一行,则替换一行中的值

[英]Replace the value in a row if his value his greater than in the previous row in my DataFrame (Pandas)

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.我有一个 DataFrame,并且在确定的列中,如果该值大于前一行,我想替换一行中的值。

The steps I am following are:我遵循的步骤是:

  1. df1 = pd.read_csv("M10_10.txt") (Reading my CSV) df1 = pd.read_csv("M10_10.txt") (读取我的 CSV)
  2. In that CSV, there is a column named m_Crit200.在该 CSV 文件中,有一列名为 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.这是我正在使用的“if”代码,但不起作用。 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

You can use shift for this:您可以为此使用shift

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

Since you want a non-increasing Series, you can use an expanding window and choose the minimum value for each window:由于您想要一个非递增系列,您可以使用expanding窗口并为每个窗口选择最小值:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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