jim jarnac
jim jarnac

Reputation: 5152

Pandas reindex a Series

I have a Series such as follow:

indic={'indicator_name': {0: '10-Year Note Auction', 
                         1: '10-Year TIPS Auction', 
                         2: '2-Year Note Auction', 
                         3: '3-Month Bill Auction', 
                         4: '3-Year Note Auction'}}

ind_serie=pd.Series(indic)

I would like to reindex it so that the values are also the index (ie : 0: '10-Year Note Auction' becomes '10-Year Note Auction': '10-Year Note Auction', etc.)

If ind_serie was a DataFrame i would use:

ind_serie.set_index('indicator_name', drop=False, inplace=True)

The reindex function also doesnt seem to work.

ind_serie.reindex(index='indicator_name') 

But what is the correct syntax to use in the case of a Series?

Upvotes: 0

Views: 2386

Answers (3)

jim jarnac
jim jarnac

Reputation: 5152

ind_serie.index = ind_serie.values didn't work properly

ind_serie.to_frame.set_index('indicator_name', drop=False, inplace=True) was the way to go eventually

Upvotes: 0

user4322543
user4322543

Reputation:

You have a dict of dicts that creates a Series with one element containing a list of the first dict. That's a different problem.

Below I create what you want starting with a list of the items.

s1 = pd.Series(
        ['10-Year TIPS Auction',
         '2-Year Note Auction',
         '3-Month Bill Auction'], name='s1')

s2 = s1.copy(deep=True).rename('s2')
s3 = pd.DataFrame(s1.values, index=s2, columns=['s1'])


s3
                                        s1
s2                                        
10-Year TIPS Auction  10-Year TIPS Auction
2-Year Note Auction    2-Year Note Auction
3-Month Bill Auction  3-Month Bill Auction

another way...

s3 = pd.DataFrame(s1, columns=['s1'])
s3.index = s1.values
s3
                                        s1
10-Year TIPS Auction  10-Year TIPS Auction
2-Year Note Auction    2-Year Note Auction
3-Month Bill Auction  3-Month Bill Auction

Upvotes: 1

piRSquared
piRSquared

Reputation: 294348

Reassign the index

ind_serie.index = ind_serie.values

Upvotes: 1

Related Questions