简体   繁体   中英

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

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 .

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 = .

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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM