text_plain_output_4.png application_vnd.jupyter.stderr_output_3.png application_vnd.jupyter.stderr_output_5.png text_plain_output_2.png text_plain_output_1.png
import numpy as np

import pandas as pd

app_ev = pd.read_csv('../input/app_events.csv', dtype={'device_id': np.str})
app_ev = app_ev.groupby('event_id')['app_id'].apply(lambda x: ' '.join(set(('app_id:' + str(s) for s in x))))

print('# Read Events')
events = pd.read_csv('../input/events.csv', dtype={'device_id': np.str})
events['app_id'] = events['event_id'].map(app_ev)
events = events.dropna()
del app_ev
events = events[['device_id', 'app_id']]
events = events.groupby('device_id')['app_id'].apply(lambda x: ' '.join(set(str(' '.join((str(s) for s in x))).split(' '))))
events = events.reset_index(name='app_id')
events = pd.concat([pd.Series(row['device_id'], row['app_id'].split(' ')) for _, row in events.iterrows()]).reset_index()
events.columns = ['app_id', 'device_id']