第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在python中查找連續(xù)出現(xiàn)的帶有條件的值

如何在python中查找連續(xù)出現(xiàn)的帶有條件的值

動漫人物 2022-07-05 17:02:39
我在熊貓中有以下數(shù)據(jù)框 code      tank     date         time       no_operation_flag 123       1        01-01-2019   00:00:00   1 123       1        01-01-2019   00:30:00   1 123       1        01-01-2019   01:00:00   0 123       1        01-01-2019   01:30:00   1 123       1        01-01-2019   02:00:00   1 123       1        01-01-2019   02:30:00   1 123       1        01-01-2019   03:00:00   1 123       1        01-01-2019   03:30:00   1 123       1        01-01-2019   04:00:00   1 123       1        01-01-2019   05:00:00   1                    123       1        01-01-2019   14:00:00   1                      123       1        01-01-2019   14:30:00   1                   123       1        01-01-2019   15:00:00   1                   123       1        01-01-2019   15:30:00   1                   123       1        01-01-2019   16:00:00   1                     123       1        01-01-2019   16:30:00   1                   123       2        02-01-2019   00:00:00   1 123       2        02-01-2019   00:30:00   0 123       2        02-01-2019   01:00:00   0 123       2        02-01-2019   01:30:00   0 123       2        02-01-2019   02:00:00   1 123       2        02-01-2019   02:30:00   1 123       2        02-01-2019   03:00:00   1 123       2        03-01-2019   03:30:00   1 123       2        03-01-2019   04:00:00   1 123       1        03-01-2019   14:00:00   1 123       2        03-01-2019   15:00:00   1 123       2        03-01-2019   00:30:00   1 123       2        04-01-2019   11:00:00   1 123       2        04-01-2019   11:30:00   0 123       2        04-01-2019   12:00:00   1 123       2        04-01-2019   13:30:00   1 123       2        05-01-2019   03:00:00   1 123       2        05-01-2019   03:30:00   1 123       2        05-01-2019   04:00:00   1我想要做的是no_operation_flag在坦克級別和日級別標記連續(xù) 1 超過 5 次,但時間應(yīng)該是連續(xù)的(時間是半小時級別)。Dataframe 已經(jīng)在容器、日期和時間級別進行了排序。
查看完整描述

3 回答

?
守著星空守著你

TA貢獻1799條經(jīng)驗 獲得超8個贊

我認為這是一種非常具有前瞻性且有些骯臟的方式,但很容易理解。

  1. 對于行循環(huán),4 行后的檢查時間是 2 小時遠。

  2. (如果 1 為真)檢查所有對應(yīng)的五個值df['no_operation_flag']都是 1。

  3. (如果 2 為真)將 1 放入對應(yīng)的 5 個值中df['final_flag']。

# make col with zero

df['final_flag'] = 0


for i in range(1, len(df)-4):

    j = i + 4

    dt1 = df['date'].iloc[i]+' '+df['time'].iloc[i]

    ts1 = pd.to_datetime(dt1)

    dt2 = df['date'].iloc[j]+' '+df['time'].iloc[j]

    ts2 = pd.to_datetime(dt2)


    # timedelta is 2 hours?

    if ts2 - ts1 == datetime.timedelta(hours=2, minutes=0):

        # all of no_operation_flag == 1?

        if (df['no_operation_flag'].iloc[i:j+1] == 1).all():

            df['final_flag'].iloc[i:j+1] = 1


查看完整回答
反對 回復 2022-07-05
?
慕尼黑5688855

TA貢獻1848條經(jīng)驗 獲得超2個贊

您可以使用這樣的解決方案,僅使用新助手過濾每個組的連續(xù)日期時間,DataFrame并添加所有缺少的日期merge時間,最后添加新列:


df['datetimes'] = pd.to_datetime(df['date'].astype(str) + ' ' + df['time'].astype(str))

df1 = (df.set_index('datetimes')

          .groupby(['code','tank', 'date'])['no_operation_flag']

          .resample('30T')

          .first()

          .reset_index())


shifted1 = df1.groupby(['code','tank', 'date'])['no_operation_flag'].shift()

g1 = df1['no_operation_flag'].ne(shifted1).cumsum()

mask1 = g1.map(g1.value_counts()).gt(5) & df1['no_operation_flag'].eq(1)


df1['final_flag'] = mask1.astype(int)

#print (df1.head(40))


df = df.merge(df1[['code','tank','datetimes','final_flag']]).drop('datetimes', axis=1)

print (df)

    code  tank        date      time  no_operation_flag  final_flag

0    123     1  01-01-2019  00:00:00                  1           0

1    123     1  01-01-2019  00:30:00                  1           0

2    123     1  01-01-2019  01:00:00                  0           0

3    123     1  01-01-2019  01:30:00                  1           1

4    123     1  01-01-2019  02:00:00                  1           1

5    123     1  01-01-2019  02:30:00                  1           1

6    123     1  01-01-2019  03:00:00                  1           1

7    123     1  01-01-2019  03:30:00                  1           1

8    123     1  01-01-2019  04:00:00                  1           1

9    123     1  01-01-2019  05:00:00                  1           0

10   123     1  01-01-2019  14:00:00                  1           1

11   123     1  01-01-2019  14:30:00                  1           1

12   123     1  01-01-2019  15:00:00                  1           1

13   123     1  01-01-2019  15:30:00                  1           1

14   123     1  01-01-2019  16:00:00                  1           1

15   123     1  01-01-2019  16:30:00                  1           1

16   123     2  02-01-2019  00:00:00                  1           0

17   123     2  02-01-2019  00:30:00                  0           0

18   123     2  02-01-2019  01:00:00                  0           0

19   123     2  02-01-2019  01:30:00                  0           0

20   123     2  02-01-2019  02:00:00                  1           0

21   123     2  02-01-2019  02:30:00                  1           0

22   123     2  02-01-2019  03:00:00                  1           0

23   123     2  03-01-2019  03:30:00                  1           0

24   123     2  03-01-2019  04:00:00                  1           0

25   123     1  03-01-2019  14:00:00                  1           0

26   123     2  03-01-2019  15:00:00                  1           0

27   123     2  03-01-2019  00:30:00                  1           0

28   123     2  04-01-2019  11:00:00                  1           0

29   123     2  04-01-2019  11:30:00                  0           0

30   123     2  04-01-2019  12:00:00                  1           0

31   123     2  04-01-2019  13:30:00                  1           0

32   123     2  05-01-2019  03:00:00                  1           0

33   123     2  05-01-2019  03:30:00                  1           0

34   123     2  05-01-2019  04:00:00                  1           0


查看完整回答
反對 回復 2022-07-05
?
江戶川亂折騰

TA貢獻1851條經(jīng)驗 獲得超5個贊

利用:


df['final_flag'] = ( df.groupby([df['no_operation_flag'].ne(1).cumsum(),

                                 'tank',

                                 'date',

                                 pd.to_datetime(df['time'].astype(str))

                                   .diff()

                                   .ne(pd.Timedelta(minutes = 30))

                                   .cumsum(),

                                'no_operation_flag'])['no_operation_flag']

                    .transform('size')

                    .gt(5)

                    .view('uint8') )

print(df)

輸出


    code  tank        date      time  no_operation_flag  final_flag

0    123     1  01-01-2019  00:00:00                  1           0

1    123     1  01-01-2019  00:30:00                  1           0

2    123     1  01-01-2019  01:00:00                  0           0

3    123     1  01-01-2019  01:30:00                  1           1

4    123     1  01-01-2019  02:00:00                  1           1

5    123     1  01-01-2019  02:30:00                  1           1

6    123     1  01-01-2019  03:00:00                  1           1

7    123     1  01-01-2019  03:30:00                  1           1

8    123     1  01-01-2019  04:00:00                  1           1

9    123     1  01-01-2019  05:00:00                  1           0

10   123     1  01-01-2019  14:00:00                  1           1

11   123     1  01-01-2019  14:30:00                  1           1

12   123     1  01-01-2019  15:00:00                  1           1

13   123     1  01-01-2019  15:30:00                  1           1

14   123     1  01-01-2019  16:00:00                  1           1

15   123     1  01-01-2019  16:30:00                  1           1

16   123     2  02-01-2019  00:00:00                  1           0

17   123     2  02-01-2019  00:30:00                  0           0

18   123     2  02-01-2019  01:00:00                  0           0

19   123     2  02-01-2019  01:30:00                  0           0

20   123     2  02-01-2019  02:00:00                  1           0

21   123     2  02-01-2019  02:30:00                  1           0

22   123     2  02-01-2019  03:00:00                  1           0

23   123     2  03-01-2019  03:30:00                  1           0

24   123     2  03-01-2019  04:00:00                  1           0

25   123     1  03-01-2019  14:00:00                  1           0

26   123     2  03-01-2019  15:00:00                  1           0

27   123     2  03-01-2019  00:30:00                  1           0

28   123     2  04-01-2019  11:00:00                  1           0

29   123     2  04-01-2019  11:30:00                  0           0

30   123     2  04-01-2019  12:00:00                  1           0

31   123     2  04-01-2019  13:30:00                  1           0

32   123     2  05-01-2019  03:00:00                  1           0

33   123     2  05-01-2019  03:30:00                  1           0



查看完整回答
反對 回復 2022-07-05
  • 3 回答
  • 0 關(guān)注
  • 170 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號