Use cumcount on pandas dataframe with a conditional increment
Use groupby
with combination of shift
and cumsum
.
df['new'] = df.groupby('key').cond.apply(
lambda x: x.shift().fillna(1).cumsum()
).astype(int)
df
key cond new
0 A 1 1
1 A 1 2
2 B 1 1
3 B 0 2
4 A 0 3
5 A 1 3
6 B 1 2
Conditional grouped CumCount pandas
You may need create a addtional key for groupby
, by using cumsum
after shfit
addkey=df.groupby(['user','#product']).action.apply(lambda x : x.eq('buy').shift().fillna(0).cumsum())
df['seebefore']=df.action.eq('see').groupby([df.user,df['#product'],addkey]).cumsum()
df
Out[131]:
index user #product action seebefore
0 0 A 1 see 1.0
1 1 A 1 see 2.0
2 2 B 2 see 1.0
3 3 A 2 see 1.0
4 4 A 1 buy 2.0
5 5 B 2 buy 1.0
6 6 A 1 see 1.0
7 7 A 2 see 2.0
8 8 A 1 see 2.0
9 9 A 1 see 3.0
10 10 A 2 buy 2.0
11 11 B 1 buy 0.0
12 12 A 1 buy 3.0
pandas - increment cumulative sum only when string is matched
You can replace Value
where Condition
is not equal to New
with 0 and then do cumulative sum:
df['Cumulative Value'] = df.Value.where(df.Condition == 'New', 0).groupby(df.Name).cumsum()
df
Name Condition Value Cumulative Value
0 A New 5 5
1 A Not New 7 5
2 A New 15 20
3 B Old 20 0
4 B New 14 14
5 B Old 3 14
how to increment a value every time a certain condition is met in pandas dataframe
You can assign()
the result of each cumsum()
to its own column:
df = df.assign(**{f'sum_{v}': df.val.eq(v).cumsum() for v in df.val.unique()})
# time val sum_1 sum_2 sum_3
# 0 0 1 1 0 0
# 1 1 1 2 0 0
# 2 2 2 2 1 0
# 3 3 3 2 1 1
# 4 4 1 3 1 1
# 5 5 2 3 2 1
pandas conditional cumcount of values in second column?
You want to get the cumcount and add one. Then use %2
to differentiate between odd or even rows. Then, take the cumulative sum and subtract 1 to start counting from zero.
You can use:
df0['flag'] = ((df0.groupby('KEY').cumcount() + 1) % 2).cumsum() - 1
df0
Out[1]:
KEY flag
0 0 0
1 0 0
2 0 1
3 0 1
4 1 2
5 1 2
6 1 3
7 2 4
8 2 4
9 2 5
10 2 5
11 2 6
12 3 7
13 3 7
14 3 8
15 3 8
16 3 9
17 3 9
18 4 10
19 5 11
20 6 12
Increase Count for Conditional Values in Row
Compare value and use cumulative sum by Series.cumsum
:
df['new'] = (df["B"]=="Yes").cumsum()
print (df)
A B new
0 WalMart Yes 1
1 Target NaN 1
2 WalMart Yes 2
3 BestBuy NaN 2
4 Target Yes 3
With some rows before first Yes
:
df['new'] = (df["B"]=="Yes").cumsum()
print (df)
A B new
0 gap NaN 0
1 WalMart Yes 1
2 Target NaN 1
3 WalMart Yes 2
4 BestBuy NaN 2
5 Target Yes 3
pandas: increment based on a condition in another column
You can try groupby
with ngroup
m = dataframe['text'].str.contains('##').cumsum()
dataframe['id'] = dataframe.groupby(m).ngroup() + 100
print(dataframe)
text id
0 ##weather 100
1 how is today? 100
2 we go out 100
3 ##rain 101
4 my day is rainy 101
5 I am not feeling well 101
6 rainy 101
7 blues 101
8 ##flower 102
9 the blue flower 102
10 she likes red 102
11 this flower is nice 102
pandas increment under specific condition of other column
Let us try groupby
and cumcount
:
m = df['A'] != df['A'].shift()
df['B'] = df.groupby(m.cumsum()).cumcount()
A B
0 a 0
1 b 0
2 b 1
3 b 2
4 b 3
5 d 0
Pandas increment values on groupby with a condition
You can try cumcount
with //
s = df.groupby('links').cumcount()//3
Out[125]:
0 0
1 0
2 0
3 0
4 0
5 1
6 1
7 0
8 0
dtype: int64
df['links'] = df['links'] + s.astype(str)
Related Topics
How to Export a Table Dataframe in Pyspark to Csv
Beautifulsoup Findall() Given Multiple Classes
Python Login Script; Usernames and Passwords in a Separate File
How to Get a Fields Particular Value of Json in Python
How to Test If a Column Exists and Is Not Null in a Dataframe
How to Convert Float into Hours Minutes Seconds
How to Pad a String With Leading Zeros in Python 3
Python - How to Separate Paragraphs from Text
What Is the Correct Way to Make My Pyqt Application Quit When Killed from the Console (Ctrl-C)
Pandas: Update Column Values from Another Column If Criteria
How to Change the Foreground or Background Colour of a Tkinter Button on MAC Os X
Cv2 Image Error: Error: (-215:Assertion Failed) !Ssize.Empty() in Function 'Cv::Resize'
How to Get Python to Detect for No Input
How to Add an Attention Mechanism in Keras
How to Split Folder of Images into Test/Training/Validation Sets With Stratified Sampling
Send a File Through Sockets in Python