+ 5

Pandas Problem

I need to create a column that contains a counter based on conditions in another column. For example, I have a column "Elapsed" which contains timestamps or the string "On Time". I need a column "On Time Count" that increments by 1 each time "On Time" is found in "Elapsed". I have looked through the book "Pandas in Action" but I don't see an example of such a process.

24th Nov 2022, 5:43 PM
Goathland1111
Goathland1111 - avatar
3 Answers
+ 3
Something like this might work, depending on your index: counter = 0 for i in range(len(df)): if df.loc[i, "Elapsed"] == "On Time": counter += 1 df.loc[i, "On Time Count"] = counter
24th Nov 2022, 7:10 PM
Tibor Santa
Tibor Santa - avatar
+ 6
Goathland1111 , can you please make a sample how the dataframe looks now, and what you expect to get with the new column. thanks.
24th Nov 2022, 6:25 PM
Lothar
Lothar - avatar
+ 2
Tibor Santa Thank you very much.
26th Nov 2022, 6:55 AM
Goathland1111
Goathland1111 - avatar