I have a column in a pandas dataframe where I want to change any uninterrupted sequences of 'normal' preceding a 'large' to 'large.' I do not want to replace all instances of 'normal' in the column with 'large.' For example, I want to change the left column to the right column below:
| input | result |
|---|---|
| small | small |
| small | small |
| normal | large |
| normal | large |
| large | large |
| small | small |
| normal | normal |
| small | small |
| normal | large |
| large | large |
This is straightforward with iteration:
for i, v in df['input'].iterrows():
if v == 'large':
index = i
while df['input'].iloc(index-1) == 'normal':
df['input'].iloc(index-1) = 'large'
index -= 1
However this is inefficient. Is there a neat vectorised way to do this?