1

I have multiple data points for every day. I need to detect the first 0 of every day. I want to transform Data to the Output column.

ho

Data in reproducible format:

Date,Data,Output
1/1/2019,1,False
1/1/2019,1,False
1/1/2019,0,True
1/1/2019,0,False
1/1/2019,1,False
2/1/2019,1,False
2/1/2019,0,True
2/1/2019,1,False
3/1/2019,0,True
3/1/2019,0,False

I thought this might involve the groupby feature, but struggling to figure out how to start.

1cmanny1
  • 3
  • 1
Chris Norris
  • 197
  • 1
  • 2
  • 13

3 Answers3

3

Using duplicated:

df["output"] = ~(df[df["Data"]==0].duplicated(subset=["Date","Data"],keep="first"))
df["output"].fillna(False, inplace=True)

print (df)

#
        Date  Data  output
0  1/01/2019     1   False
1  1/01/2019     1   False
2  1/01/2019     0    True
3  1/01/2019     0   False
4  1/01/2019     1   False
5  2/01/2019     1   False
6  2/01/2019     0    True
7  2/01/2019     1   False
8  3/01/2019     0    True
9  3/01/2019     0   False
Henry Yik
  • 22,275
  • 4
  • 18
  • 40
0

Try 2 boolean masks

m = df.Data.eq(0)
m1 = m.groupby(df.Date).cumsum().eq(1)    
df['New'] = m & m1

Out[834]:
        Date  Data    New
0  1/01/2019     1  False
1  1/01/2019     1  False
2  1/01/2019     0   True
3  1/01/2019     0  False
4  1/01/2019     1  False
5  2/01/2019     1  False
6  2/01/2019     0   True
7  2/01/2019     1  False
8  3/01/2019     0   True
9  3/01/2019     0  False
Andy L.
  • 24,909
  • 4
  • 17
  • 29
0

Another groupby solution with loc

df.loc[df[df.data.eq(0)].groupby('date').data.idxmin(), 'out'] = True
df = df.fillna(False)
manwithfewneeds
  • 1,137
  • 1
  • 7
  • 10