9. 重命名行名和列名

  1. # 通过rename()重命名
  2. In[88]: movie = pd.read_csv('data/movie.csv', index_col='movie_title')
  3. In[89]: idx_rename = {'Avatar':'Ratava', 'Spectre': 'Ertceps'}
  4. col_rename = {'director_name':'Director Name',
  5. 'num_critic_for_reviews': 'Critical Reviews'}
  6. In[90]: movie.rename(index=idx_rename,
  7. columns=col_rename).head()
  8. Out[90]:

9. 重命名行名和列名 - 图1

更多

  1. In[91]: movie = pd.read_csv('data/movie.csv', index_col='movie_title')
  2. index = movie.index
  3. columns = movie.columns
  4. index_list = index.tolist()
  5. column_list = columns.tolist()
  6. index_list[0] = 'Ratava'
  7. index_list[2] = 'Ertceps'
  8. column_list[1] = 'Director Name'
  9. column_list[2] = 'Critical Reviews'
  10. In[92]: print(index_list[:5])
  11. ['Ratava', "Pirates of the Caribbean: At World's End", 'Ertceps', 'The Dark Knight Rises', 'Star Wars: Episode VII - The Force Awakens']
  12. In[93]: print(column_list)
  13. ['color', 'Director Name', 'Critical Reviews', 'duration', 'director_facebook_likes', 'actor_3_facebook_likes', 'actor_2_name', 'actor_1_facebook_likes', 'gross', 'genres', 'actor_1_name', 'num_voted_users', 'cast_total_facebook_likes', 'actor_3_name', 'facenumber_in_poster', 'plot_keywords', 'movie_imdb_link', 'num_user_for_reviews', 'language', 'country', 'content_rating', 'budget', 'title_year', 'actor_2_facebook_likes', 'imdb_score', 'aspect_ratio', 'movie_facebook_likes']
  14. In[94]: movie.index = index_list
  15. movie.columns = column_list
  16. In[95]: movie.head()
  17. Out[95]:

9. 重命名行名和列名 - 图2