Skip to content

Instantly share code, notes, and snippets.

@erap129
Created January 29, 2022 18:43
Show Gist options
  • Select an option

  • Save erap129/0ffccc8bbaf66af9023628b6640861f5 to your computer and use it in GitHub Desktop.

Select an option

Save erap129/0ffccc8bbaf66af9023628b6640861f5 to your computer and use it in GitHub Desktop.
readme_text = np.array(open(f'{BASE_FOLDER}/MovieLens-1M/README').read().splitlines())
start_index = np.flatnonzero(np.core.defchararray.find(readme_text,'Occupation is chosen')!=-1)[0]
end_index = np.flatnonzero(np.core.defchararray.find(readme_text,'MOVIES FILE DESCRIPTION')!=-1)[0]
occupation_list = [x.split('"')[1] for x in readme_text[start_index:end_index][2:-1].tolist()]
occupation_dict = dict(zip(range(len(occupation_list)), occupation_list))
users_df = pd.read_csv(f'{BASE_FOLDER}/MovieLens-1M/users.dat',
delimiter='::', engine='python', header=None,
names=['user_id', 'gender', 'age', 'occupation', 'zip_code'])
users_df['occupation'] = users_df['occupation'].replace(occupation_dict)
users_df.head()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment