rsajdak
rsajdak

Reputation: 103

Iterating a suffix using apply in pandas

I'm trying to use the apply function in Pandas with a lambda to create a named value.

I have the TIME column and I want the ACT_NUM column as shown in the following.

df
TIME    ACT_NUM
3       act_1
3       act_1
4       act_1
12      act_2
3       act_2
15      act_3

The logic of the wanted column: When the value in the TIME column is > 10, then I would like to increment the suffix number by 1 permanently.

I have tried this, but it was not working because I couldn't integrate the variable n row to row when using apply.

n = 1
df['ACT_NUM'] = df['TIME'].apply(lambda x: 'act_'+(n+1) if x>10 else 'act_'+n)

Upvotes: 0

Views: 120

Answers (2)

Bill Huang
Bill Huang

Reputation: 4658

df.cumsum() on df["TIME"] > 10 seems to be what you are looking for. The cumsum begins from 0 and increase by 1 whenever a row with df["TIME"] > 10 is encountered.

Code

df["ACT_NUM"] = (df["TIME"] > 10).cumsum().apply(lambda el: f"act_{el + 1}")

Result

print(df)
   TIME ACT_NUM
0     3   act_1
1     3   act_1
2     4   act_1
3    12   act_2
4     3   act_2
5    15   act_3

Upvotes: 1

Nour-Allah Hussein
Nour-Allah Hussein

Reputation: 1459

Just use str(n+1) and str(n) to convert number to string :

df['ACT_NUM'] = df['TIME'].apply(lambda x: 'act_'+str(n+1) if x>10 else 'act_'+str(n))

Upvotes: 0

Related Questions