A

#### AmirX

##### Guest

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)
```

<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>