首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pandas reindex a Series

Pandas reindex a Series
EN

Stack Overflow用户
提问于 2016-12-29 10:12:12
回答 3查看 1.9K关注 0票数 0

我有一个类似如下的系列:

代码语言:javascript
复制
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)

我想重新索引它,这样值也是索引(例如:0: '10-Year Note Auction'变成'10-Year Note Auction': '10-Year Note Auction',依此类推)

如果ind_serie是一个DataFrame,我会使用:

代码语言:javascript
复制
ind_serie.set_index('indicator_name', drop=False, inplace=True)

reindex函数似乎也不起作用。

代码语言:javascript
复制
ind_serie.reindex(index='indicator_name') 

但是,在使用Series的情况下,正确的语法是什么?

EN

回答 3

Stack Overflow用户

发布于 2016-12-29 11:38:40

重新分配索引

代码语言:javascript
复制
ind_serie.index = ind_serie.values
票数 1
EN

Stack Overflow用户

发布于 2016-12-29 11:56:34

你有一个字典,它用一个包含第一个字典列表的元素来创建一个序列。这是一个不同的问题。

下面我从一个项目列表开始创建你想要的东西。

代码语言:javascript
复制
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

另一种方式...

代码语言:javascript
复制
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
票数 1
EN

Stack Overflow用户

发布于 2016-12-30 10:52:43

ind_serie.index = ind_serie.values不能正常工作

ind_serie.to_frame.set_index('indicator_name', drop=False, inplace=True)是最终要走的路

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41372247

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档