2. Advanced indexing - upalr/Python-camp GitHub Wiki

1 Index objects and labeled data

1.1 Pandas data structure

1 pandas-datastructure

1.2 Creating a series

2 creating-a-series

1.3 Creating a index

3 creating-a-index

1.4 Examining an index

4 examining-an-index

1.5 Modifying index nameindivi

5 modifying-an-index

1.6 Modifying index entries

Notice an exception is raised when attempting to assign to individual index entries or index slices. Index entries are immutable like dictionary keys. This restriction helps pandas optimize operation on series and dataframes.

6 modify-index-entries

It is technically possible to reassign to index by overriding it all at once. Notice that doing so also erases the name of the index.

7 modify-index-entries-2

1.7 Unemployment data

8 unemployment-data

9 unemployment-data-2

1.8 Assign the index

10 assigning-the -index

1.9 Removing extra column

11 removig-extra-column

1.10 Examining index & columns

12 examining

1.11 read_csv() with index_col

13 read_csvwithindex

1.12 Example : Building an index, then a DataFrame

You can also build the DataFrame and index independently, and then put them together. If you take this route, be careful, as any mistakes in generating the DataFrame or the index can cause the data and the index to be aligned incorrectly.

Building an index, then a DataFrame

2 Hierarchical indexing

2.1 Stock data

14 stock data

2.2 Setting index

15 setting-index

2.3 MultiIndex on DataFrame

16 multiindex-on-dataframes

2.4 Sorting index

17 sorting-index

Info: With a MultiIndex, you should always ensure the index is sorted. You can skip this only if you know the data is already sorted on the index fields.

2.5 Indexing (Individual row)

18 indexing individual row

2.6 Slicing (outermost index)

19 slicing outermost-index

20 slicing outermost-index -2

2.7 Fancy-indexing(outermost-index)

21 fancy-indexing outermost-index

2.8 Fancy-indexing(innermost-index)

22 fancy-indexing innermost-index

2.9 Slicing (both indexes)

There is a trick to slicing with hierarchical indexes. The tuple use for the index doesn't recognize slicing with colons (:) naively. To force that to work we need to use the python built in slice explicitly. Foe example:

23 sclicing both-indexes