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

Python function Assign values based on another column and then based on the row below [duplicate]

  • Thread starter Thread starter Albert Ginard
  • Start date Start date
A

Albert Ginard

Guest
Given a value in a column, I want to assign to this column the value shown in the same row on another column. When this condition is not met,for the blanks, I want them to be fulfilled with the row below.

I want to assign in the column "Newest agreement code" the value in the column agreement code only when the column Old/New = 'New'

When the column Old/New = 'Old' then I want it to be fulfilled with the row below in "Newest agreement code".

The code is not returning anything on the rows where the value in "Old/New" = 'Old'

df["Newest agreement code"] = ""

Code:
# Iterate through rows (excluding the last row to avoid IndexError)
for i in range(len(df) - 1):
    # Check if the current row's "Old/New" is "New"
    if df.loc[i, "Old/New"] == "New":
        # Assign the current row's "Agreement Code" to "Newest agreement code"
        df.loc[i, "Newest agreement code"] = df.loc[i, "Agreement Code"]
    else:
        # Check if there's a next row (avoid IndexError)
        if i < len(df) - 1:
            next_row_newest_code = df.loc[i + 1, "Newest agreement code"]
            if next_row_newest_code:
                # Assign next row's "Newest agreement code" if it exists
                df.loc[i, "Newest agreement code"] = next_row_newest_code

return df

enter image description here
<p>Given a value in a column, I want to assign to this column the value shown in the same row on another column. When this condition is not met,for the blanks, I want them to be fulfilled with the row below.</p>
<p>I want to assign in the column "Newest agreement code" the value in the column agreement code only when the column Old/New = 'New'</p>
<p>When the column Old/New = 'Old' then I want it to be fulfilled with the row below in "Newest agreement code".</p>
<p>The code is not returning anything on the rows where the value in "Old/New" = 'Old'</p>
<p>df["Newest agreement code"] = ""</p>
<pre><code># Iterate through rows (excluding the last row to avoid IndexError)
for i in range(len(df) - 1):
# Check if the current row's "Old/New" is "New"
if df.loc[i, "Old/New"] == "New":
# Assign the current row's "Agreement Code" to "Newest agreement code"
df.loc[i, "Newest agreement code"] = df.loc[i, "Agreement Code"]
else:
# Check if there's a next row (avoid IndexError)
if i < len(df) - 1:
next_row_newest_code = df.loc[i + 1, "Newest agreement code"]
if next_row_newest_code:
# Assign next row's "Newest agreement code" if it exists
df.loc[i, "Newest agreement code"] = next_row_newest_code

return df
</code></pre>
<p><a href="https://i.sstatic.net/BHUbZiMz.png" rel="nofollow noreferrer">enter image description here</a></p>
 
Top