python - Filter data using pandas -
i have data
id date sec buy 5211153 2016-06-13 18:48:55 119 1 5211153 2016-06-13 18:50:54 66 0 5211153 2016-06-13 18:57:09 2 1 5211154 2016-06-13 18:57:12 118 0 5211154 2016-06-13 18:59:08 20 0 5211154 2016-06-13 18:59:34 25 0 5211154 2016-06-13 18:59:59 11 0 5211154 2016-06-13 19:00:11 12 1
i want print data buy = 0
before every buy = 1
. try code
for i, (id, date, buy) in enumerate(zip(ids, dates, buys)): if buys[i] == 1: print ids[i], dates[i] while buys[i-1] != 1: print dates[i - 1], buys[i - 1]
but return not want desire output:
5211153 2016-06-13 18:57:09 1: 5211153 2016-06-13 18:50:54 5211154 2016-06-13 19:00:11 1: 5211154 2016-06-13 18:57:12 5211154 2016-06-13 18:59:08 5211154 2016-06-13 18:59:34 5211154 2016-06-13 18:59:59
update: should work current sample df, i'm not sure have alternate ones , zeroes in buy
column (for example if have following sequence in buy
column belonging same id
: 1,0,1,1,0 - desired output in case?)
in [20]: df.sort_values(['id', 'date'],ascending=[1,0])[df.sort_values(['id', 'date'], ascending=[1,0]).groupby('id')['buy'].cumsum() < 2] out[20]: id date sec buy 2 5211153 2016-06-13 18:57:09 2 1 1 5211153 2016-06-13 18:50:54 66 0 7 5211154 2016-06-13 19:00:11 12 1 6 5211154 2016-06-13 18:59:59 11 0 5 5211154 2016-06-13 18:59:34 25 0 4 5211154 2016-06-13 18:59:08 20 0 3 5211154 2016-06-13 18:57:12 118 0
old answer:
iiuc can way:
df.loc[(df.buy==0) & (df.buy.shift(-1)==1)]
yields:
in [77]: df.loc[(df.buy==0) & (df.buy.shift(-1)==1)] out[77]: id date sec buy 1 5211153 2016-06-13 18:50:54 66 0 6 5211154 2016-06-13 18:59:59 11 0
please post desired data set if need else
Comments
Post a Comment