我有一个类似如下的系列:
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,我会使用:
ind_serie.set_index('indicator_name', drop=False, inplace=True)reindex函数似乎也不起作用。
ind_serie.reindex(index='indicator_name') 但是,在使用Series的情况下,正确的语法是什么?
发布于 2016-12-29 11:38:40
重新分配索引
ind_serie.index = ind_serie.values发布于 2016-12-29 11:56:34
你有一个字典,它用一个包含第一个字典列表的元素来创建一个序列。这是一个不同的问题。
下面我从一个项目列表开始创建你想要的东西。
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另一种方式...
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发布于 2016-12-30 10:52:43
ind_serie.index = ind_serie.values不能正常工作
ind_serie.to_frame.set_index('indicator_name', drop=False, inplace=True)是最终要走的路
https://stackoverflow.com/questions/41372247
复制相似问题