#!/usr/bin/env python # coding: utf-8 # In[68]: import pandas as pd import numpy as np import datetime as dt from datetime import datetime as dt # ## Say one has a lookup table summarizing the busy lives of a few people on this planet... # In[69]: t=pd.Timestamp lu = pd.DataFrame({ 'name' : ['Bill','Elon','Larry','Jeff','Marissa'], 'feels' : ['charitable','Alcoa envy','Elon envy','like the number 7','sassy'], 'last ate' : [t('20151209'),t('20151201'),t('20151208'),t('20151208'),t('20151209')], 'boxers' : [True,True,True,False,True]}) # ## Say one also knows where these people live and when they did certain things... # In[70]: af = pd.DataFrame({ 'name' : ['Bill','Elon','Larry','Elon','Jeff','Larry','Larry'], 'address' : ['in my computer','moon','internet','mars','cardboard box','autonomous car','every where'], 'sq_ft' : [2,2135,69,84535, 1.32, 54,168], 'forks' : [7,1,2,1,0,np.nan,1]}) rand_dates=[t('20141202'),t('20130804'),t('20120508'),t('20150411'), t('20141209'),t('20091023'),t('20130921'),t('20110102'), t('20130728'),t('20141119'),t('20151024'),t('20130824')] df = pd.DataFrame({ 'name' : ['Elon','Bill','Larry','Elon','Jeff','Larry','Larry','Bill','Larry','Elon','Marissa','Jeff'], 'activity' : ['slept','tripped','spoke','swam','spooked','liked','whistled','up dog','smiled','donated','grant men paternity leave','fondled'], 'date' : rand_dates}) # In[71]: lu # In[ ]: af # In[ ]: df # ## One could rank these people according to addresses they live at as follows: # In[74]: #af.groupby(['name','address']).size() af.name.value_counts() # ## Need 1: Using the ranking above, how would one create a new "ranked" dataframe composed of information from lookup table lu? aka How to make Exhibit A? # In[75]: # Exhibit A # boxers feels last ate name addresses #0 True Elon envy 2015-12-08 Larry 3 #1 True Alcoa envy 2015-12-01 Elon 2 #2 False like the number 7 2015-12-08 Jeff 1 #3 True charitable 2015-12-09 Bill 1 # In[ ]: # In[ ]: # ## Need 2: Observe the output of the groupby operation that follows. How can one determine the time delta between the oldest and newest dates to rank members of lu according to such time deltas? aka How to get from the groupby to Exhibit D? # In[76]: df.groupby(['name','date']).size() # In[ ]: # Exhibit B - Calculate time deltas # name time_delta # Bill Timedelta('945 days 00:00:00') # Elon Timedelta('143 days 00:00:00') # Jeff Timedelta('472 days 00:00:00') # Larry Timedelta('1429 days 00:00:00') # Marissa Timedelta('0 days 00:00:00') # Exhibit C - Rank time deltas (this is easy) # name time_delta # Larry Timedelta('1429 days 00:00:00') # Bill Timedelta('945 days 00:00:00') # Jeff Timedelta('472 days 00:00:00') # Elon Timedelta('143 days 00:00:00') # Marissa Timedelta('0 days 00:00:00') # Exhibit D - Add to and re-rank the table built in Exhibit A according to time_delta # boxers feels last ate name addresses time_delta # 0 True Elon envy 2015-12-08 Larry 3 1429 days 00:00:00 # 1 True charitable 2015-12-09 Bill 1 945 days 00:00:00 # 2 False like the number 7 2015-12-08 Jeff 1 472 days 00:00:00 # 3 True Alcoa envy 2015-12-01 Elon 2 143 days 00:00:00 # 4 True sassy 2015-12-09 Marissa NaN 0 days 00:00:00 # In[ ]: # In[ ]: # In[ ]: # In[ ]: