-
Notifications
You must be signed in to change notification settings - Fork 0
/
Left Join raw and hits
51 lines (41 loc) · 1.86 KB
/
Left Join raw and hits
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# This scripts will:
# a. create a left join between the raw data (songs) and the hits
# b. filter out all those observations where 'year' is not in 'year' hits table
import pandas as pd
import numpy as np
from pandas import ExcelWriter
#defining the paths to the datasets
path_raw = r"C:\\Users\\giovanni\\Desktop\\Spotify Final Project\data.xlsx"
path_hits = r"C:\\Users\\giovanni\\Desktop\\Spotify Final Project\datasets_456949_861300_top10s.xlsx"
#assigning the dataframe to the related variables
raw = pd.read_excel(path_raw)
hits = pd.read_excel(path_hits)
#Dataset exploration
print("Shape of Spotify data",raw.shape)
print("Shape of Spotify Hits list",hits.shape)
min_date= min(hits['year'])
max_date= max(hits['year'])
count_raw = raw.shape[0]
print("Date range for Hits dataset: \n", min_date, '--',max_date)
##First step###
print("Filtering out from the raw data years which do not fall into the hits year range")
year_list = []
for year in hits['year']:
year_list.append(year)
raw_filtered = raw[raw['year'].isin(year_list)]
count_raw_filtered=raw_filtered.shape[0]
raw_diff= count_raw -count_raw_filtered
print("Total deleted observations: ", raw_diff)
#I want to join the 2 tables on titles and name
#I will first drop all the columns from hits that are not relevant and keep only the titles
#For every match, we will have a hit
#Note: songs in raw are 'name' whereas in hits they are 'title'
hits=hits['title']
df_merged = pd.merge(raw_filtered, hits, left_on='name', right_on='title',how = 'left')
#adding boolean values to Hits
#First I need to replace NaN
df_merged['title'].fillna('', inplace = True)
df_merged['Hits'] = np.where(df_merged['title']=='',False,True)
#Writing the new dataset
with pd.ExcelWriter(r"C:\\Users\\giovanni\\Desktop\\Spotify Final Project\\Mario_Iuliano_FP\raw_data_cleaned.xlsx") as writer:
df_merged.to_excel(writer,index= False )