Skip to content

Instantly share code, notes, and snippets.

@thushw
Created April 5, 2018 00:06
Show Gist options
  • Save thushw/3d58a4f5d7f614c3bd288412e4b0d8ed to your computer and use it in GitHub Desktop.
Save thushw/3d58a4f5d7f614c3bd288412e4b0d8ed to your computer and use it in GitHub Desktop.
import datetime
df = pd.DataFrame({'Month': [datetime.date(2008, i, 1).strftime('%B') for i in range(1,13)] * 3,
'visited': [False]*36},
index= ['Alice']*12 + ['Bob']*12 + ['Bridgett']*12)
df = df.reset_index()
df [(df['index'] == 'Alice') &
((df['Month'] == 'February') | (df['Month'] == 'March') | (df['Month'] == 'April'))]
def make_regular(r, name):
r['visited'] = (r['visited'] or (r['index'] == name) and
((r['Month'] == 'February') or (r['Month'] == 'March') or (r['Month'] == 'April')))
return r
df = df.apply(make_regular, axis=1, args=('Alice',))
df = df.apply(make_regular, axis=1, args=('Bob',))
regular = (df['visited'] == True) & (df['visited'].shift(-1) == True) & (df['visited'].shift(-2) == True)
df[regular]['index'].values .tolist()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment