Reputation: 383
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.
Upvotes: 0
Views: 212
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