@@ -868,66 +868,6 @@ convert to an integer index:
868
868
df_new[(df_new[' index' ] >= 1.0 ) & (df_new[' index' ] < 2 )]
869
869
870
870
871
- .. _indexing.class :
872
-
873
- Index objects
874
- -------------
875
-
876
- The pandas Index class and its subclasses can be viewed as implementing an
877
- *ordered set * in addition to providing the support infrastructure necessary for
878
- lookups, data alignment, and reindexing. The easiest way to create one directly
879
- is to pass a list or other sequence to ``Index ``:
880
-
881
- .. ipython :: python
882
-
883
- index = Index([' e' , ' d' , ' a' , ' b' ])
884
- index
885
- ' d' in index
886
-
887
- You can also pass a ``name `` to be stored in the index:
888
-
889
-
890
- .. ipython :: python
891
-
892
- index = Index([' e' , ' d' , ' a' , ' b' ], name = ' something' )
893
- index.name
894
-
895
- Starting with pandas 0.5, the name, if set, will be shown in the console
896
- display:
897
-
898
- .. ipython :: python
899
-
900
- index = Index(list (range (5 )), name = ' rows' )
901
- columns = Index([' A' , ' B' , ' C' ], name = ' cols' )
902
- df = DataFrame(np.random.randn(5 , 3 ), index = index, columns = columns)
903
- df
904
- df[' A' ]
905
-
906
-
907
- Set operations on Index objects
908
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
909
-
910
- .. _indexing.set_ops :
911
-
912
- The three main operations are ``union (|) ``, ``intersection (&) ``, and ``diff
913
- (-) ``. These can be directly called as instance methods or used via overloaded
914
- operators:
915
-
916
- .. ipython :: python
917
-
918
- a = Index([' c' , ' b' , ' a' ])
919
- b = Index([' c' , ' e' , ' d' ])
920
- a.union(b)
921
- a | b
922
- a & b
923
- a - b
924
-
925
- ``isin `` method of Index objects
926
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
927
-
928
- One additional operation is the ``isin `` method that works analogously to the
929
- ``Series.isin `` method found :ref: `here <indexing.boolean >`.
930
-
931
871
.. _indexing.hierarchical :
932
872
933
873
Hierarchical indexing (MultiIndex)
@@ -1189,7 +1129,7 @@ are named.
1189
1129
1190
1130
.. ipython :: python
1191
1131
1192
- s.index.names = [' L1' , ' L2' ]
1132
+ s.index.set_names( [' L1' , ' L2' ], inplace = True )
1193
1133
s.sortlevel(level = ' L1' )
1194
1134
s.sortlevel(level = ' L2' )
1195
1135
@@ -1276,6 +1216,88 @@ not check (or care) whether the levels themselves are sorted. Fortunately, the
1276
1216
constructors ``from_tuples `` and ``from_arrays `` ensure that this is true, but
1277
1217
if you compute the levels and labels yourself, please be careful.
1278
1218
1219
+ .. _indexing.class :
1220
+
1221
+ Index objects
1222
+ -------------
1223
+
1224
+ The pandas Index class and its subclasses can be viewed as implementing an
1225
+ *ordered set * in addition to providing the support infrastructure necessary for
1226
+ lookups, data alignment, and reindexing. The easiest way to create one directly
1227
+ is to pass a list or other sequence to ``Index ``:
1228
+
1229
+ .. ipython :: python
1230
+
1231
+ index = Index([' e' , ' d' , ' a' , ' b' ])
1232
+ index
1233
+ ' d' in index
1234
+
1235
+ You can also pass a ``name `` to be stored in the index:
1236
+
1237
+
1238
+ .. ipython :: python
1239
+
1240
+ index = Index([' e' , ' d' , ' a' , ' b' ], name = ' something' )
1241
+ index.name
1242
+
1243
+ Starting with pandas 0.5, the name, if set, will be shown in the console
1244
+ display:
1245
+
1246
+ .. ipython :: python
1247
+
1248
+ index = Index(list (range (5 )), name = ' rows' )
1249
+ columns = Index([' A' , ' B' , ' C' ], name = ' cols' )
1250
+ df = DataFrame(np.random.randn(5 , 3 ), index = index, columns = columns)
1251
+ df
1252
+ df[' A' ]
1253
+
1254
+
1255
+ Set operations on Index objects
1256
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1257
+
1258
+ .. _indexing.set_ops :
1259
+
1260
+ The three main operations are ``union (|) ``, ``intersection (&) ``, and ``diff
1261
+ (-) ``. These can be directly called as instance methods or used via overloaded
1262
+ operators:
1263
+
1264
+ .. ipython :: python
1265
+
1266
+ a = Index([' c' , ' b' , ' a' ])
1267
+ b = Index([' c' , ' e' , ' d' ])
1268
+ a.union(b)
1269
+ a | b
1270
+ a & b
1271
+ a - b
1272
+
1273
+ ``isin `` method of Index objects
1274
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1275
+
1276
+ One additional operation is the ``isin `` method that works analogously to the
1277
+ ``Series.isin `` method found :ref: `here <indexing.boolean >`.
1278
+
1279
+ Setting index metadata (``name(s) ``, ``levels ``, ``labels ``)
1280
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1281
+
1282
+ .. _indexing.set_metadata :
1283
+
1284
+ Indexes are "mostly immutable", but it is possible to set and change their
1285
+ metadata, like the index ``name `` (or, for ``MultiIndex ``, ``levels `` and
1286
+ ``labels ``).
1287
+
1288
+ You can use the ``rename ``, ``set_names ``, ``set_levels ``, and ``set_labels ``
1289
+ to set these attributes directly. They default to returning a copy; however,
1290
+ you can specify ``inplace=True `` to have the data change inplace.
1291
+
1292
+ .. ipython :: python
1293
+
1294
+ ind = Index([1 , 2 , 3 ])
1295
+ ind.rename(" apple" )
1296
+ ind
1297
+ ind.set_names([" apple" ], inplace = True )
1298
+ ind.name = " bob"
1299
+ ind
1300
+
1279
1301
Adding an index to an existing DataFrame
1280
1302
----------------------------------------
1281
1303
0 commit comments