Sneha Lakshmi Roy
Sneha Lakshmi Roy

Reputation: 15

Is there a pythonic way of incrementing values of a column based on values from another column?

I want to assign incremental values to a column. However, the increment is not continuous, the row at which the increment takes place is dependent on the value of another column.

Currently I am using a for loop for this which painstakingly slow. I am getting the result I want as shown below. Can you suggest a more pythonic way to do this?

a=1
for index, row in df.iterrows():
    df.loc[index,'trip_id'] = a
    if df.loc[index,'interim_tour_chk'] >= 0:
        a = a+1

my desired results

Upvotes: 0

Views: 116

Answers (4)

BENY
BENY

Reputation: 323226

I feel like only cumsum will not solve your problem , before doing that we need do diff

df['trip_id']=(df.interim_tourchk.diff()<0).cumsum()
df
    interim_tourchk  trip_id
0                 0        0
1                 1        0
2                 0        1
3                 1        1
4                 0        2
5                 1        2
6                 0        3
7                 0        3
8                 0        3
9                 1        3
10                0        4
11                0        4
12                0        4
13                1        4

Upvotes: 0

Pinyi Wang
Pinyi Wang

Reputation: 862

You can try this:

df['trip_id'] = (df['interim_tour_chk'] == 0).cumsum()

Explanation:

(df['interim_tour_chk'] == 0) will return a pandas series of boolean of whether each 'interim_tour_chk' is equals to 0. And here's the documentation of pandas's cumsum.

Upvotes: 1

perigon
perigon

Reputation: 2095

If I'm interpreting correctly, you want the value of df.trip_id to increase by 1 each time df.interim_tourchk is 0.

This will get the job done:

df['trip_id'] = (df.interim_tourchk == 0).cumsum()

(and subtract 1 if you want it to start from 0).

Upvotes: 0

knh190
knh190

Reputation: 2882

You don't need using index though:

a = 1
for idx, row in df.iterrows():
    row['trip_id'] = a
    if row['interim_tour_chk'] == 0:
        a += 1

Also mind your comparison operator is == not assign operator =.

Upvotes: 0

Related Questions