Search code examples
pythonpandasseries

How do I change the index values of a Pandas Series?


How can I change the index values of a Pandas Series from the regular integer value that they default to, to values within a list that I have? e.g.

x = pd.Series([421, 122, 275, 847, 175])

index_values = ['2014-01-01', '2014-01-02', '2014-01-03', '2014-01-04',
               '2014-01-05'] 

How do I get the dates in the index_values list to be the indexes in the Series that I've created?


Solution

  • You can assign index values by list:

    x.index = index_values
    print(x)
    2014-01-01    421
    2014-01-02    122
    2014-01-03    275
    2014-01-04    847
    2014-01-05    175
    dtype: int64