How to change indexing in python

DataFrame.set_index(keys, drop=True, append=False, inplace=False, verify_integrity=False)[source]#

Set the DataFrame index using existing columns.

Set the DataFrame index (row labels) using one or more existing columns or arrays (of the correct length). The index can replace the existing index or expand on it.

Parameterskeyslabel or array-like or list of labels/arrays

This parameter can be either a single column key, a single array of the same length as the calling DataFrame, or a list containing an arbitrary combination of column keys and arrays. Here, “array” encompasses Series, Index, np.ndarray, and instances of Iterator.

dropbool, default True

Delete columns to be used as the new index.

appendbool, default False

Whether to append columns to existing index.

inplacebool, default False

Whether to modify the DataFrame rather than creating a new one.

verify_integritybool, default False

Check the new index for duplicates. Otherwise defer the check until necessary. Setting to False will improve the performance of this method.

ReturnsDataFrame or None

Changed row labels or None if inplace=True.

Examples

>>> df = pd.DataFrame({'month': [1, 4, 7, 10],
...                    'year': [2012, 2014, 2013, 2014],
...                    'sale': [55, 40, 84, 31]})
>>> df
   month  year  sale
0      1  2012    55
1      4  2014    40
2      7  2013    84
3     10  2014    31

Set the index to become the ‘month’ column:

>>> df.set_index('month')
       year  sale
month
1      2012    55
4      2014    40
7      2013    84
10     2014    31

Create a MultiIndex using columns ‘year’ and ‘month’:

>>> df.set_index(['year', 'month'])
            sale
year  month
2012  1     55
2014  4     40
2013  7     84
2014  10    31

Create a MultiIndex using an Index and a column:

>>> df.set_index([pd.Index([1, 2, 3, 4]), 'year'])
         month  sale
   year
1  2012  1      55
2  2014  4      40
3  2013  7      84
4  2014  10     31

Create a MultiIndex using two Series:

>>> s = pd.Series([1, 2, 3, 4])
>>> df.set_index([s, s**2])
      month  year  sale
1 1       1  2012    55
2 4       4  2014    40
3 9       7  2013    84
4 16     10  2014    31

What I mean by this is may I change the calling index of an array. For example, if I have array a = [1,2,3] can I make it so a[1] = 1? I know this is an option in FORTRAN which certainly keeps things organized and legible. Thanks

asked Sep 25, 2014 at 3:28

9

Changing the indexing of a list in Python is almost definitely not what you want to do as it will break len(some_list), lead to off-by-one errors of valid input, and iteration. Starting at index[1] runs counter to most of the language's design.

Python's lists are implemented on top of C arrays (see listobject.c in the Python source), which are indexed starting at 0.

If you're positive that you need to start counting at one, consider initializing your lists by setting a[0] to None so that you're it's clear that you're inserting a dummy value.

answered Sep 25, 2014 at 3:36

9

Although in general it is inadvisable, you are free to extend built-in types, for example:

# Python 3 syntax
class FiveList(list):
    def __getitem__(self, idx):
        return super().__getitem__(idx - 5)

>>> FiveList(range(100))[10]
5

You'd have to implement a whole lot of magic methods (below), and also check if argument is int-like (index) or a slice.

__getitem__
__setitem__
__delitem__

Also, watch out for negative indices, as those are special in Python.

answered Sep 25, 2014 at 7:52

Dima TisnekDima Tisnek

10.7k4 gold badges62 silver badges116 bronze badges

Without knowing more about your application is is difficult to help, but it may be useful for you to read Emulating Container Types.

I suggest that if you were to write a class, say BasedArray, that had an integer base attribute that specified the index of the first element, and a normal zero-based list that held the data. Then you could get away with writing just the __len__, __get item__ and __setitem__ methods, which would be trivial, and probably __iter__ which is less so. The exact set of methods you need depends on what you want to do with these based arrays once you have them.

answered Sep 25, 2014 at 4:06

BorodinBorodin

125k9 gold badges69 silver badges143 bronze badges

I assume you mean start index at 1, instead of 0?

Why not just put 0 at the start? E.g.

 [0,1,2,3]

answered Sep 25, 2014 at 3:34

smushismushi

7036 silver badges17 bronze badges

2

How do you change the index of a column in Python?

You can use the rename() method of pandas. DataFrame to change column/index name individually. Specify the original name and the new name in dict like {original name: new name} to columns / index parameter of rename() . columns is for the column name, and index is for the index name.

Can we change index in DataFrame?

Pandas DataFrame: set_index() function The set_index() function is used to set the DataFrame index using existing columns. Set the DataFrame index (row labels) using one or more existing columns or arrays of the correct length. The index can replace the existing index or expand on it.

Is Python 1 or 0 index?

Note. python lists are 0-indexed. So the first element is 0, second is 1, so on. So if the there are n elements in a list, the last element is n-1.

How do I change the index name in a data frame?

Pandas rename() method is used to rename any index, column or row..
Syntax: rename(mapper=None, index=None, columns=None, axis=None, copy=True, inplace=False, level=None).
Parameters:.
Return Type: Data frame with new names..

How do I change an index to a column?

In order to set index to column in pandas DataFrame use reset_index() method. By using this you can also set single, multiple indexes to a column. If you are not aware by default, pandas adds an index to each row of the pandas DataFrame.

How can you change the index of a Pandas Series syntax?

It can be done by using the index attribute of the pandas series constructor..
Example..
Explanation. pd is pandas package alias name defined while importing the pandas package. ... .
Output. 0 1 1 2 2 3 3 4 4 6 dtype: int64 Series with new index A 1 B 2 C 3 D 4 E 6 dtype: int64. ... .
Example. ... .
Explanation. ... .
Output..