Skip to Content
Numerical Computing with Python
book

Numerical Computing with Python

by Pratap Dangeti, Allen Yu, Claire Chung, Aldrin Yim
December 2018
Beginner to intermediate
682 pages
18h 1m
English
Packt Publishing
Content preview from Numerical Computing with Python

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: ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering Numerical Computing with NumPy

Mastering Numerical Computing with NumPy

Umit Mert Cakmak, Tiago Antao, Mert Cuhadaroglu

Publisher Resources

ISBN: 9781789953633OtherOtherErrata Page