简体   繁体   English

根据循环条件创建一个新列(while、list id、counter、timestamp...)

[英]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计数器从 0 开始
  2. For each rows, if I have an end => counter +=1 else (I have an begin) => counter -=1对于每一行,如果我有一个结束 => 计数器 +=1 否则(我有一个开始)=> 计数器 -=1

  3. When counter == 0 => save the id of the timestamp in a list当 counter == 0 => 将时间戳的 id 保存在列表中

  4. When counter = 0, The next row must be saved当 counter = 0 时,必须保存下一行
  5. When the loop is finish, fill the new column 'New_Timestamp with the values ​​corresponding to the id of the column Timestamp.当循环结束时,用与 Timestamp 列的 id 对应的值填充新列 'New_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)...因为: 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.目前我无法将相应的值添加到 ID 中,也许我在代码中忘记了 (a) 个条件。

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 :根据您的逻辑,只需将end替换为1并以-1 begin ,然后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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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