@@ -503,6 +503,47 @@ method, allowing you to permute the hierarchical index levels in one step:
503
503
504
504
df[:5 ].reorder_levels([1 ,0 ], axis = 0 )
505
505
506
+ .. _advanced.index_names :
507
+
508
+ Renaming names of an ``Index `` or ``MultiIndex ``
509
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
510
+
511
+ The :meth: `~DataFrame.rename ` method is used to rename the labels of a
512
+ ``MultiIndex ``, and is typically used to rename the columns of a ``DataFrame ``.
513
+ The ``columns `` argument of ``rename `` allows a dictionary to be specified
514
+ that includes only the columns you wish to rename.
515
+
516
+ .. ipython :: python
517
+
518
+ df.rename(columns = {0 : " col0" , 1 : " col1" })
519
+
520
+ This method can also be used to rename specific labels of the main index
521
+ of the ``DataFrame ``.
522
+
523
+ .. ipython :: python
524
+
525
+ df.rename(index = {" one" : " two" , " y" : " z" })
526
+
527
+ The :meth: `~DataFrame.rename_axis ` method is used to rename the name of a
528
+ ``Index `` or ``MultiIndex ``. In particular, the names of the levels of a
529
+ ``MultiIndex `` can be specified, which is useful if ``reset_index() `` is later
530
+ used to move the values from the ``MultiIndex `` to a column.
531
+
532
+ .. ipython :: python
533
+
534
+ df.rename_axis(index = [' abc' , ' def' ])
535
+
536
+ Note that the columns of a ``DataFrame `` are an index, so that using
537
+ ``rename_axis `` with the ``columns `` argument will change the name of that
538
+ index.
539
+
540
+ .. ipython :: python
541
+
542
+ df.rename_axis(columns = " Cols" ).columns
543
+
544
+ Both ``rename `` and ``rename_axis `` support specifying a dictionary,
545
+ ``Series `` or a mapping function to map labels/names to new values.
546
+
506
547
Sorting a ``MultiIndex ``
507
548
------------------------
508
549
@@ -738,15 +779,17 @@ values **not** in the categories, similarly to how you can reindex **any** panda
738
779
Reshaping and Comparison operations on a ``CategoricalIndex `` must have the same categories
739
780
or a ``TypeError `` will be raised.
740
781
741
- .. code-block :: python
782
+ .. code-block :: ipython
783
+
784
+ In [9]: df3 = pd.DataFrame({'A' : np.arange(6),
785
+ 'B' : pd.Series(list('aabbca')).astype('category')})
742
786
743
- >> > df3 = pd.DataFrame({' A' : np.arange(6 ),
744
- ... ' B' : pd.Series(list (' aabbca' )).astype(' category' )})
745
- >> > df3 = df3.set_index(' B' )
746
- >> > df3.index
747
- CategoricalIndex([u ' a' , u ' a' , u ' b' , u ' b' , u ' c' , u ' a' ], categories = [u ' a' , u ' b' , u ' c' ], ordered = False , name = u ' B' , dtype = ' category' )
787
+ In [11]: df3 = df3.set_index('B')
748
788
749
- >> > pd.concat([df2, df3])
789
+ In [11]: df3.index
790
+ Out[11]: CategoricalIndex([u'a', u'a', u'b', u'b', u'c', u'a'], categories=[u'a', u'b', u'c'], ordered=False, name=u'B', dtype='category')
791
+
792
+ In [12]: pd.concat([df2, df3]
750
793
TypeError: categories must match existing categories when appending
751
794
752
795
.. _indexing.rangeindex :
@@ -1028,14 +1071,14 @@ On the other hand, if the index is not monotonic, then both slice bounds must be
1028
1071
# OK because 2 and 4 are in the index
1029
1072
df.loc[2 :4 , :]
1030
1073
1031
- .. code-block :: python
1074
+ .. code-block :: ipython
1032
1075
1033
1076
# 0 is not in the index
1034
- >> > df.loc[0 :4 , :]
1077
+ In [9]: df.loc[0:4, :]
1035
1078
KeyError: 0
1036
1079
1037
1080
# 3 is not a unique label
1038
- >> > df.loc[2 :3 , :]
1081
+ In [11]: df.loc[2:3, :]
1039
1082
KeyError: 'Cannot get right slice bound for non-unique label: 3'
1040
1083
1041
1084
``Index.is_monotonic_increasing `` and ``Index.is_monotonic_decreasing `` only check that
0 commit comments