Skip to content Skip to sidebar Skip to footer

How To Get The Oldest Part Installation Of Consecutive Installations In The Same Boat Where Column X Is False?

I got a nice algorithm that returns the oldest part installation of consecutive installations in the same boat: How to get oldest part installation of consecutive installations the

Solution 1:

One idea is create new helper column filled by unique values if Trues with DataFrame.assign and Index.where and processing all 3 columns:

df1 = df.assign(tmp = df.index.where(df['x'], -1))[['boat','part','tmp']]
df = df[df1.ne(df1.shift()).any(axis=1)]
print (df)
  boat part            date      x
0    A    E  12/20/13 03:27   True
1    C    E    1/8/16 17:08   True
2    C    E   3/23/16 17:15   True
3    B    E    3/5/18 22:35  False

And for second:

df1 = df.assign(tmp = df.index.where(df['x'], -1))[['boat','part','tmp']]
df = df[df1.ne(df1.shift()).any(axis=1)]
print (df)
  boat part            date      x
0    A    E  12/20/13 03:27   True
1    B    E   7/21/14 09:46  False
2    C    E    1/8/16 17:08   True
3    C    E   3/23/16 17:15   True
4    B    E    3/5/18 22:35  False

Post a Comment for "How To Get The Oldest Part Installation Of Consecutive Installations In The Same Boat Where Column X Is False?"