How to Remember Pandas Index Methods
Tags: python, tutorial, machinelearning, pandas
Published: 2019-07-19 18:09:08

When method names are similar, it's difficult to keep them separate in your mind. 
This makes remembering them harder. 

Pandas has a slew of methods for creating and adjusting a DataFrame index.
This is a brief guide to help you create a little mental space between methods for easier memorization.


The Jupyter Notebook is on Kaggle [here](https://www.kaggle.com/discdiver/how-to-remember-pandas-index-methods/).


```python
import pandas as pd
import numpy as np
```

### Make a DataFrame without specifying an index (you get a default index).


```python
df = pd.DataFrame(dict(a=[1,2,3,4], b=[2,5,6,4]))
df
```

<div>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>a</th>
      <th>b</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>1</td>
      <td>2</td>
    </tr>
    <tr>
      <th>1</th>
      <td>2</td>
      <td>5</td>
    </tr>
    <tr>
      <th>2</th>
      <td>3</td>
      <td>6</td>
    </tr>
    <tr>
      <th>3</th>
      <td>4</td>
      <td>4</td>
    </tr>
  </tbody>
</table>
</div>



### Make a DataFrame with an index by using the [index](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html) keyword argument.


```python
df2 = pd.DataFrame(dict(a=[1,2,3,4], b=[2,5,6,4]), index = [1,2,5,6])
df2
```

<div>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>a</th>
      <th>b</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>1</th>
      <td>1</td>
      <td>2</td>
    </tr>
    <tr>
      <th>2</th>
      <td>2</td>
      <td>5</td>
    </tr>
    <tr>
      <th>5</th>
      <td>3</td>
      <td>6</td>
    </tr>
    <tr>
      <th>6</th>
      <td>4</td>
      <td>4</td>
    </tr>
  </tbody>
</table>
</div>



### Move a column to be the index with [.set_index()](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.set_index.html#pandas.DataFrame.set_index)



```python
df3 = df2.set_index("a")
df3
```

<div>

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>b</th>
    </tr>
    <tr>
      <th>a</th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>1</th>
      <td>2</td>
    </tr>
    <tr>
      <th>2</th>
      <td>5</td>
    </tr>
    <tr>
      <th>3</th>
      <td>6</td>
    </tr>
    <tr>
      <th>4</th>
      <td>4</td>
    </tr>
  </tbody>
</table>
</div>



### Rename the index values from scratch with [.index](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Index.html)


```python
df3.index = [2,3,4,5]
df3
```


<div>

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>b</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>2</th>
      <td>2</td>
    </tr>
    <tr>
      <th>3</th>
      <td>5</td>
    </tr>
    <tr>
      <th>4</th>
      <td>6</td>
    </tr>
    <tr>
      <th>5</th>
      <td>4</td>
    </tr>
  </tbody>
</table>
</div>


Note that `index` is a property of the DataFrame not a method, so the syntax is different.

### Nuke the index values and start over from 0 with [.reset_index()](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.reset_index.html#pandas.DataFrame.reset_index)


```python
df4 = df3.reset_index()
df4
```


<div>

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>index</th>
      <th>b</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>2</td>
      <td>2</td>
    </tr>
    <tr>
      <th>1</th>
      <td>3</td>
      <td>5</td>
    </tr>
    <tr>
      <th>2</th>
      <td>4</td>
      <td>6</td>
    </tr>
    <tr>
      <th>3</th>
      <td>5</td>
      <td>4</td>
    </tr>
  </tbody>
</table>
</div>



If you don't want the index to become a column, pass `drop=True` to `reset_index()`.


```python
df5 = df3.reset_index(drop=True)
df5
```


<div>

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>b</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>2</td>
    </tr>
    <tr>
      <th>1</th>
      <td>5</td>
    </tr>
    <tr>
      <th>2</th>
      <td>6</td>
    </tr>
    <tr>
      <th>3</th>
      <td>4</td>
    </tr>
  </tbody>
</table>
</div>



### Reorder the rows with [.reindex()](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.reindex.html#pandas.DataFrame.reindex)


```python
df6 = df5.reindex([2,3,1,0])
df6
```

<div>

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>b</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>2</th>
      <td>6</td>
    </tr>
    <tr>
      <th>3</th>
      <td>4</td>
    </tr>
    <tr>
      <th>1</th>
      <td>5</td>
    </tr>
    <tr>
      <th>0</th>
      <td>2</td>
    </tr>
  </tbody>
</table>
</div>



Passing a value that isn't in the index results in a NaN.


```python
df7 = df5.reindex([2,3,1,0,6])
df7
```

<div>

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>b</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>2</th>
      <td>6.0</td>
    </tr>
    <tr>
      <th>3</th>
      <td>4.0</td>
    </tr>
    <tr>
      <th>1</th>
      <td>5.0</td>
    </tr>
    <tr>
      <th>0</th>
      <td>2.0</td>
    </tr>
    <tr>
      <th>6</th>
      <td>NaN</td>
    </tr>
  </tbody>
</table>
</div>



## Advice

Ideally, add an index when you create your DataFrame with `index = `. 

If reading from a .csv file you can set an index column by passing the column number. 

For example:

`df = pd.read_csv(my_csv, index_col=3)`

Or pass `index_col=False` to exlcude.



## How to set or change the index:

- `df.set_index()` - move a column to the index 

- `df.index` - add an index manually

- `df.reset_index()` - reset the index to *0, 1, 2 ...*

- `df.reindex()` - reorder the rows


## Word associations to remember:

- `set_index()` - move column

- `index` - manual

- `reset_index()` - reset

- `reindex` - reorder

## Wrap

I hope this article helped you create a little mental space to keep Pandas index methods straight. If it did, please give it some love so other people can find it, too.

I write about Data Science, Dev Ops, Python and other stuff. Check out my other [articles](https://medium.com/@jeffhale) if any of that sounds interesting.

Follow me and connect:
[Medium](https://medium.com/@jeffhale)
[Dev.to](https://dev.to/discdiver)
[Twitter](https://twitter.com/discdiver)
[LinkedIn](https://www.linkedin.com/in/-jeffhale)
[Kaggle](https://www.kaggle.com/discdiver)
[GitHub](https://github.com/discdiver)

![Reset Button](https://thepracticaldev.s3.amazonaws.com/i/gd6kiza5mgsh470nruhi.jpg)

Happy indexing!