I am trying to automate sth but struggling with it. It is actually my first code in Python and what I would like to order this data frame by Hotel Id (which is already done) and then create a column that says:
- Look into the column “Hotel Id” and if the ID in row below is equal to the ID in current row, the write “Old”, otherwise “New”.
In this image we can see how the code gave the value “Old” in row whose ID=23
This is the code I am using and it is asigning values in the incorrect rows(see picture).:
def asignar_old_new(df):
df['Old/New'] = 'New'
for i in range(1,len(df) - 1):
if df['Hotel Id'].iloc[i] == df['Hotel Id'].iloc[i-1]:
df.loc[i, 'Old/New'] = 'Old'
return df
Albert Ginard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.