How to do it...

  1. We will be using the versatile wide_to_long function to reshape our data into tidy form. To use this function, we will need to change the column names that we are stacking, so that they end with a digit. We first create a user-defined function to change the column names:
>>> def change_col_name(col_name):
col_name = col_name.replace('_name', '')
if 'facebook' in col_name:
fb_idx = col_name.find('facebook')
col_name = col_name[:5] + col_name[fb_idx - 1:]
+ col_name[5:fb_idx-1]
return col_name
  1. Pass this function to the rename method to transform all the column names:
>>> actor2 = actor.rename(columns=change_col_name)
>>> actor2.head()
  1. Use the wide_to_long function to stack the actor and Facebook sets of columns simultaneously:
>>> stubs = ['actor', 'actor_facebook_likes']
>>> actor2_tidy = pd.wide_to_long(actor2,
stubnames=stubs,
i=['movie_title'],
j='actor_num',
sep='_')
>>> actor2_tidy.head()
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.214.27