<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Data Exploration
Step1:  Data Cleaning
Remove invalid first column RespondentID which are NaN.
Step2:  Change the second and third columns.
Step3:  The values for the second and third columns which are Have you seen any of the 6 films in the Star Wars franchise? and Do you consider yourself to be a fan of the Star Wars film franchise? respectively are Yes, No, NaN. We want to change them to True or False.
Step4:  Cleaning the columns from index 3 to 9.
From the fourth column to ninth columns are checkbox questions
Step5:  Rename the columns from index 3 to 9 for better readibility.
seen_1 means Star Wars Episode I, and so on.
Step6:  Cleaning the columns from index 9 to 15.
Changing data type to float.
Step7:  Renaming columns names.
Step8:  Cleaning the cloumns from index 15 to 29.
Step9:  Data Analysis
Finding The Most Seen Movie
Step10:  From the data above, we can find that the most seen movie is the episode V.
Step11:  Finding The Highest Ranked Movie.
Step12:  The highest ranked movie is ranking_5 which is the episode V.
Step13:  Let's break down data by Gender.
Step14:  The number of movies seen.
Step15:  The ranking of movies.
Step16:  From the charts above, we do not find significant difference among gender.
Star Wars Character Favorability Ratings
Step17:  Convert fav to pivot table.
Step18:  Who Shot First?
<ASSISTANT_TASK:>
Python Code:
star_wars = pd.read_csv('star_wars.csv', encoding="ISO-8859-1")
star_wars.head()
star_wars.columns
Explanation: Data Exploration
End of explanation
star_wars = star_wars.dropna(subset=['RespondentID'])
Explanation: Data Cleaning
Remove invalid first column RespondentID which are NaN.
End of explanation
star_wars['Do you consider yourself to be a fan of the Star Wars film franchise?'].isnull().value_counts()
star_wars['Have you seen any of the 6 films in the Star Wars franchise?'].value_counts()
Explanation: Change the second and third columns.
End of explanation
star_wars['Have you seen any of the 6 films in the Star Wars franchise?'] = star_wars['Have you seen any of the 6 films in the Star Wars franchise?'].map({'Yes': True, 'No': False})
star_wars['Do you consider yourself to be a fan of the Star Wars film franchise?'] = star_wars['Do you consider yourself to be a fan of the Star Wars film franchise?'].map({'Yes': True, 'No': False})
Explanation: The values for the second and third columns which are Have you seen any of the 6 films in the Star Wars franchise? and Do you consider yourself to be a fan of the Star Wars film franchise? respectively are Yes, No, NaN. We want to change them to True or False.
End of explanation
for col in star_wars.columns[3:9]:
    star_wars[col] = star_wars[col].apply(lambda x: False if pd.isnull(x) else True)
Explanation: Cleaning the columns from index 3 to 9.
From the fourth column to ninth columns are checkbox questions:
 If values are the movie names: they have seen the movies.
 If values are NaN: they have not seen the movies.  
We are going to convert the values of these columns to bool type.
End of explanation
star_wars.rename(columns={'Which of the following Star Wars films have you seen? Please select all that apply.': 'seen_1', \
                          'Unnamed: 4': 'seen_2', \
                          'Unnamed: 5': 'seen_3', \
                          'Unnamed: 6': 'seen_4', \
                          'Unnamed: 7': 'seen_5', \
                          'Unnamed: 8': 'seen_6'}, inplace=True)
Explanation: Rename the columns from index 3 to 9 for better readibility.
seen_1 means Star Wars Episode I, and so on.
End of explanation
star_wars[star_wars.columns[9:15]] = star_wars[star_wars.columns[9:15]].astype(float)
Explanation: Cleaning the columns from index 9 to 15.
Changing data type to float.
End of explanation
star_wars.rename(columns={'Please rank the Star Wars films in order of preference with 1 being your favorite film in the franchise and 6 being your least favorite film.': 'ranking_1', \
                          'Unnamed: 10': 'ranking_2', \
                          'Unnamed: 11': 'ranking_3', \
                          'Unnamed: 12': 'ranking_4', \
                          'Unnamed: 13': 'ranking_5', \
                          'Unnamed: 14': 'ranking_6'}, inplace=True)
Explanation: Renaming columns names.
End of explanation
star_wars.rename(columns={'Please state whether you view the following characters favorably, unfavorably, or are unfamiliar with him/her.': 'Luck Skywalker', \
                          'Unnamed: 16': 'Han Solo', \
                          'Unnamed: 17': 'Princess Leia Oragana', \
                          'Unnamed: 18': 'Obi Wan Kenobi', \
                          'Unnamed: 19': 'Yoda', \
                          'Unnamed: 20': 'R2-D2', \
                          'Unnamed: 21': 'C-3P0', \
                          'Unnamed: 22': 'Anakin Skywalker', \
                          'Unnamed: 23': 'Darth Vader', \
                          'Unnamed: 24': 'Lando Calrissian', \
                          'Unnamed: 25': 'Padme Amidala', \
                          'Unnamed: 26': 'Boba Fett', \
                          'Unnamed: 27': 'Emperor Palpatine', \
                          'Unnamed: 28': 'Jar Jar Binks'}, inplace=True)
Explanation: Cleaning the cloumns from index 15 to 29.
End of explanation
seen_sum = star_wars[['seen_1', 'seen_2', 'seen_3', 'seen_4', 'seen_5', 'seen_6']].sum()
seen_sum
seen_sum.idxmax()
Explanation: Data Analysis
Finding The Most Seen Movie
End of explanation
ax = seen_sum.plot(kind='bar') 
for p in ax.patches:
    ax.annotate(str(p.get_height()), (p.get_x() * 1.005, p.get_height() * 1.01))
plt.show()
Explanation: From the data above, we can find that the most seen movie is the episode V.
End of explanation
ranking_mean = star_wars[['ranking_1', 'ranking_2', 'ranking_3', 'ranking_4', 'ranking_5', 'ranking_6']].mean()
ranking_mean
ranking_mean.idxmin()
Explanation: Finding The Highest Ranked Movie.
End of explanation
ranking_mean.plot(kind='bar')
plt.show()
Explanation: The highest ranked movie is ranking_5 which is the episode V.
End of explanation
males = star_wars[star_wars['Gender'] == 'Male']
females = star_wars[star_wars['Gender'] == 'Female']
Explanation: Let's break down data by Gender.
End of explanation
males[males.columns[3:9]].sum().plot(kind='bar', title='male seen')
plt.show()
males[females.columns[3:9]].sum().plot(kind='bar', title='female seen')
plt.show()
Explanation: The number of movies seen.
End of explanation
males[males.columns[9:15]].mean().plot(kind='bar', title='Male Ranking')
plt.show()
females[males.columns[9:15]].mean().plot(kind='bar', title='Female Ranking')
plt.show()
Explanation: The ranking of movies.
End of explanation
star_wars['Luck Skywalker'].value_counts()
star_wars[star_wars.columns[15:29]].head()
fav = star_wars[star_wars.columns[15:29]].dropna()
fav.head()
Explanation: From the charts above, we do not find significant difference among gender.
Star Wars Character Favorability Ratings
End of explanation
fav_df_list = []
for col in fav.columns.tolist():
    row = fav[col].value_counts()
    d1 = pd.DataFrame(data={'favorably': row[0] + row[1], \
                   'neutral': row[2], \
                   'unfavorably': row[4] + row[5], \
                   'Unfamiliar': row[3]}, \
                  index=[col], \
                  columns=['favorably', 'neutral', 'unfavorably', 'Unfamiliar'])
    fav_df_list.append(d1)
fav_pivot = pd.concat(fav_df_list)
fav_pivot
fig = plt.figure()
ax = plt.subplot(111)
fav_pivot.plot(kind='barh', stacked=True, figsize=(10,10), ax=ax)
# Shrink current axis's height by 10% on the bottom
box = ax.get_position()
ax.set_position([box.x0, box.y0 + box.height * 0.1,
                 box.width, box.height * 0.9])
# Put a legend below current axis
ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05),
          fancybox=True, shadow=True, ncol=5)
plt.show()
Explanation: Convert fav to pivot table.
End of explanation
shot_first = star_wars['Which character shot first?'].value_counts()
shot_first
shot_sum = shot_first.sum()
shot_first = shot_first.apply(lambda x: x / shot_sum * 100)
shot_first
ax = shot_first.plot(kind='barh')
for p in ax.patches:
    ax.annotate(str("{0:.2f}%".format(round(p.get_width(),2))), (p.get_width() * 1.005, p.get_y() + p.get_height() * 0.5))
plt.show()
Explanation: Who Shot First?
End of explanation
<END_TASK>