nielsen
nielsen

Reputation: 383

How to create an ID starting from 1 that increases by 1 every time the previous row of another column is a specific value

I'm working in Python, and I need to create a Journey ID and a Journey number. See picture for illustration. ID should increase every time previous row of the column "Purpose" takes the value 1. Journey number does the same but within each Respondent ID.

GezPerVer2019['JourneyID'] = np.where(GezPerVer2019['Hoofddoel'] = 1, GezPerVer2019['JourneyID'][i+1] + 1, GezPerVer2019['JourneyID'][i-1])

Is what I've tried. Obviously, I'm not yet too skilled at this and I think the problem is that np.where doesn't allow the [i] indicators.

Any help will be greatly appreciated.

On left before and on right expected after

Upvotes: 0

Views: 212

Answers (1)

yatu
yatu

Reputation: 88236

Use boolean indexing and cumsum here instead:

m = df['Purpose'] == 1
df.loc[m, 'JourneyID'] = m.cumsum()

Note: = is for assignment, == for comparisson. You want the latter here when comparing with 1

Upvotes: 1

Related Questions