简体   繁体   中英

Create a new column from loop condition (while, list id, counter, timestamp...)

I would like to create a new timestamp column with this dataframe :

                     Timestamp                          Flag
0  2019-10-21  07:48:28.272688                           end
1  2019-10-21  07:48:28.449916                           end 
2  2019-10-21  07:48:26.740378                         begin
3  2019-10-21  07:48:26.923764                         begin
4  2019-10-21  07:48:41.689466                           end
5  2019-10-21  07:48:37.306045                         begin
6  2019-10-21  07:58:00.774449                           end
7  2019-10-21  07:57:59.223986                         begin
8  2019-10-21  08:32:37.004455                           end
9  2019-10-21  08:32:35.755252                         begin

The principe is simple :

  1. The counter begin at 0
  2. For each rows, if I have an end => counter +=1 else (I have an begin) => counter -=1

  3. When counter == 0 => save the id of the timestamp in a list

  4. When counter = 0, The next row must be saved
  5. When the loop is finish, fill the new column 'New_Timestamp with the values ​​corresponding to the id of the column Timestamp.

So the result must be:

                     Timestamp                          Flag
0  2019-10-21  07:48:28.272688                           end 
2  2019-10-21  07:48:26.740378                         begin
3  2019-10-21  07:48:26.923764                         begin
4  2019-10-21  07:48:41.689466                           end
5  2019-10-21  07:48:37.306045                         begin
6  2019-10-21  07:58:00.774449                           end
7  2019-10-21  07:57:59.223986                         begin
8  2019-10-21  08:32:37.004455                           end
9  2019-10-21  08:32:35.755252                         begin                        

Because : First end => counter = 1 (save(first row), ct = 2, ct = 1(save), ct = 0 (save), (save) ct = 1; ct =0 (save)...

Currently I can't add the corresponding values ​​to the IDs and maybe I forgot (a) condition(s) in my code.

My Piece of code :

counter = 0
i = 0

while i < len(df):

  id_timestamp_to_save = []


  if df.loc[i, 'Flag'] == 'end':
    counter +=1
    if counter == 1:
      id_timestamp_to_save = list(range(i))


  else:
    counter -=1
    if counter == 0:
      id_timestamp_to_save = list(range(i))


  df['New_Timestamp'] = df['New_Timestamp'].assign(id_timestamp_to_save)
  i+=1

Help me please.

According to your logic, just replace end with 1 and begin with -1 , then cumsum :

counters = df.Flag.map({'end':1,'begin':-1}).cumsum().eq(0)
df[counters | counters.shift(fill_value=True)]

Output:

                    Timestamp   Flag
0 2019-10-21  07:48:28.272688    end
3 2019-10-21  07:48:26.923764  begin
4 2019-10-21  07:48:41.689466    end
5 2019-10-21  07:48:37.306045  begin
6 2019-10-21  07:58:00.774449    end
7 2019-10-21  07:57:59.223986  begin
8 2019-10-21  08:32:37.004455    end
9 2019-10-21  08:32:35.755252  begin

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