set_index(self, keys, drop: 'bool' = True, append: 'bool' = False, inplace: 'bool' = False, verify_integrity: 'bool' = False)
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.
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 ~collections.abc.Iterator
.
Delete columns to be used as the new index.
Whether to append columns to existing index.
If True, modifies the DataFrame in place (do not create a new object).
Check the new index for duplicates. Otherwise defer the check until necessary. Setting to False will improve the performance of this method.
Changed row labels or None if inplace=True
.
Set the DataFrame index using existing columns.
DataFrame.reindex
Change to new indices or expand indices.
DataFrame.reindex_like
Change to same indices as other DataFrame.
DataFrame.reset_index
Opposite of set_index.
>>> 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:
This example is valid syntax, but we were not able to check execution>>> 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':
This example is valid syntax, but we were not able to check execution>>> 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:
This example is valid syntax, but we were not able to check execution>>> 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:
This example is valid syntax, but we were not able to check execution>>> s = pd.Series([1, 2, 3, 4])See :
... 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
The following pages refer to to this document either explicitly or contain code examples using this.
pandas.core.generic.NDFrame.reindex
pandas.core.frame.DataFrame.reset_index
pandas.core.frame.DataFrame.reindex
pandas.core.series.Series.reindex
pandas.core.generic.NDFrame.reindex_like
Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.
Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)
SVG is more flexible but power hungry; and does not scale well to 50 + nodes.
All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them