application_vnd.jupyter.stderr_output_2.png application_vnd.jupyter.stderr_output_1.png
from subprocess import check_output

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import numpy as np
import pandas as pd
from subprocess import check_output
events = pd.read_csv('../input/events.csv', parse_dates=['timestamp'])
test = pd.read_csv('../input/gender_age_test.csv')
train = pd.read_csv('../input/gender_age_train.csv')

events['timestamp'].loc[(events.longitude != 0.0) & (events.latitude != 0.0)] += events['longitude'].apply(lambda x: pd.Timedelta(seconds=240 * (x - 116.407)))
events['hourly'] = events.timestamp.dt.hour
events['hourly'].loc[(events.longitude != 0.0) & (events.latitude != 0.0)] = np.nan
hourly = events.groupby('device_id')['hourly'].apply(lambda x: ' '.join((str(s) for s in x)))
train['hourly'] = 'Hourly:' + train['device_id'].map(hourly).astype(str)
test['hourly'] = 'Hourly:' + test['device_id'].map(hourly).astype(str)
print(train.loc[['device_id', 'hourly']])