I have this pandas dataframe:
df = pd.DataFrame(
{
"col1": [1,1,2,3,3,3,4,5,5,5,5]
}
)
df
I want to add another column that says "last" if the value in col1 doesnt equal the value of col1 in the next row. This is how it should look like:
So far, I can create a column that contains True when if the value in col1 doesnt equal the value of col1 in the next row; and False otherwise:
df["last_row"] = df["col1"].shift(-1)
df['last'] = df["col1"] != df["last_row"]
df = df.drop(["last_row"], axis=1)
df
Now something like
df["last_row"] = df["col1"].shift(-1)
df['last'] = "last" if df["col1"] != df["last_row"]
df = df.drop(["last_row"], axis=1)
df
would be nice, but this is apparently the wrong syntax. How can I manage to do this?
Ultimatly, I also want to add numbers that indicate how many time a value appear before this while the last value is always marked with "last". It should look like this:
I'm not sure if this is another step in my development or if this requires a new approach. I read that if I want to loop through an array while modifying values, I should use apply(). However, I don't know how to include conditions in this. Can you help me?
Thanks a lot!




df['last'][df['col1'] != df['last_row']] = 'last'.