OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How can I preserve the previous value to find the row that is greater than it?

  • Thread starter Thread starter AmirX
  • Start date Start date
A

AmirX

Guest
This is my DataFrame:

Code:
import pandas as pd
df = pd.DataFrame(
    {
        'start': [3, 11, 9, 19, 22],
        'end': [10, 17, 10, 25, 30]
    }
)

And expected output is creating column x:

Code:
   start  end    x
0      3   10    10
1     11   17    17
2      9   10    NaN
3     19   25    25
4     22   30    NaN

Logic:

I explain it row by row. For row 0, x is df.end.iloc[0]. Now this value of x needs to be preserved until a greater value is found in the next rows and in the start column.

So 10 should be saved then the process moves to row 1. Is 11 > 10? If yes then x of second row is 17. For the next row, Is 9 > 17? No so the value is NaN.

The process moves to next row. Since no values is found that is greater than 17, 17 is preserved. Is 19 > 17? Yes so x is set to 25. And for the last row since 22 < 25, NaN is selected.

This is my attempt:

Code:
l = []
for ind, row in df.iterrows():
    if ind == 0:
        x = row['end']
        continue
    if row['start'] > row['end']:
        x = row['end']
    l.append(x)
<p>This is my DataFrame:</p>
<pre><code>import pandas as pd
df = pd.DataFrame(
{
'start': [3, 11, 9, 19, 22],
'end': [10, 17, 10, 25, 30]
}
)
</code></pre>
<p>And expected output is creating column <code>x</code>:</p>
<pre><code> start end x
0 3 10 10
1 11 17 17
2 9 10 NaN
3 19 25 25
4 22 30 NaN
</code></pre>
<p>Logic:</p>
<p>I explain it row by row. For row <code>0</code>, <code>x</code> is <code>df.end.iloc[0]</code>. Now this value of <code>x</code> needs to be preserved until a greater value is found in the next rows and in the <code>start</code> column.</p>
<p>So 10 should be saved then the process moves to row <code>1</code>. Is 11 > 10? If yes then <code>x</code> of second row is 17. For the next row, Is 9 > 17? No so the value is <code>NaN</code>.</p>
<p>The process moves to next row. Since no values is found that is greater than 17, 17 is preserved. Is 19 > 17? Yes so <code>x</code> is set to 25. And for the last row since 22 < 25, <code>NaN</code> is selected.</p>
<p>This is my attempt:</p>
<pre><code>l = []
for ind, row in df.iterrows():
if ind == 0:
x = row['end']
continue
if row['start'] > row['end']:
x = row['end']
l.append(x)
</code></pre>
 

Latest posts

A
Replies
0
Views
1
Ashrik Ahamed
A
A
Replies
0
Views
1
Ashrik Ahamed
A

Online statistics

Members online
1
Guests online
4
Total visitors
5
Top