Question: CODE NOT GIVING CORRECT PERCENTAGES How People Rate the 'Star Wars' Movies How often each film was rated in the top, middle and bottom third

CODE NOT GIVING CORRECT PERCENTAGES
How People Rate the 'Star Wars' Movies
How often each film was rated in the top, middle and bottom third (by 471 respondents who have seen all six films)
** Homework note: Click here to see a version of this plot generated in Altair.
2.2 How people rate the 'Star Wars' movie? Recreate the above image using altair (10 Points)
MY CODE NOT GIVING CORRECT PERCENTAGES
def genRateVis(inpf, eps, names=namesDict, names_l=namesList):
# input: inpf, the star wars dataset
# input: eps, the list of episodes
# input: names - a dictionary of abbreviations to names (default: namesDict)
# input: names_l - a list of all the movies in series order (default: namesList)
# output: the Altair visualization as described above
names_1=[
'The Phantom Menace',
'Attack of the Clones',
'Revenge of the Sith',
'A New Hope',
'The Empire Strikes Back',
'Return of the Jedi']
ranking_cols =[f'rank_{e}' for e in eps]
inpf = inpf.dropna(subset=['seen_'+ ep for ep in eps], how='all')
inpf[ranking_cols]= inpf[ranking_cols].apply(pd.to_numeric)
total_responses = inpf.shape[0]
dfperc1=((inpf[ranking_cols]=2).sum()/ total_responses).reset_index()
dfperc1.columns =['Name', 'Percentage']
dfperc1['Name']= names_1
dfperc2=((inpf[ranking_cols].isin([3,4])).sum()/ total_responses).reset_index()
dfperc2.columns =['Name', 'Percentage']
dfperc2['Name']= names_1
dfperc3=((inpf[ranking_cols]>=5).sum()/ total_responses).reset_index()
dfperc3.columns =['Name', 'Percentage']
dfperc3['Name']= names_1
base1= alt.Chart(dfperc1).mark_bar(size=20).encode(
x=alt.X('Percentage', axis=None),
y=alt.Y('Name:N', axis=alt.Axis(tickCount=5, title=''), sort=names_l))
base2= alt.Chart(dfperc2).mark_bar(size=20).encode(
x=alt.X('Percentage', axis=None),
y=alt.Y('Name:N', axis=None, sort=names_l))
base3= alt.Chart(dfperc3).mark_bar(size=20).encode(
x=alt.X('Percentage', axis=None),
y=alt.Y('Name:N', axis=None, sort=names_l))
text1= base1.mark_text(align='left', baseline='middle', dx=3).encode(
text=alt.Text('Percentage:Q', format='.0%'))
text2= base2.mark_text(align='left', baseline='middle', dx=3).encode(
text=alt.Text('Percentage:Q', format='.0%'))
text3= base3.mark_text(align='left', baseline='middle', dx=3).encode(
text=alt.Text('Percentage:Q', format='.0%'))
mixed1=(text1+ base1).encode(color=alt.value('#00ab41')).properties(
width=100, height=180, title="Top third")
mixed2=(text2+ base2).encode(color=alt.value('#008fd5')).properties(
width=100, height=180, title="Middle third")
mixed3=(text3+ base3).encode(color=alt.value('#C41E3A')).properties(
width=100, height=180, title="Bottom third")
res = alt.hconcat(mixed1, mixed2, mixed3).configure_view(
strokeWidth=0
).properties(
title={
"text": "How People Rate the 'Star Wars' Movies",
"subtitle": "How often each film was rated in the top, middle and bottom third (by 471 respondents who have seen all six films)"
}
).configure_title(
fontSize=18, anchor='start', offset=20)
return res
# let's check our solution
genRateVis(sw,episodes)
CODE NOT GIVING CORRECT PERCENTAGES How People

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!