Python 熊猫重新编制了一系列指数
我有如下系列:Python 熊猫重新编制了一系列指数,python,pandas,Python,Pandas,我有如下系列: 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
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年期国债拍卖”
变为“10年期国债拍卖”:“10年期国债拍卖”
,等等)
如果ind_serie
是一个数据帧,我会使用:
ind_serie.set_index('indicator_name', drop=False, inplace=True)
reindex函数似乎也不起作用
ind_serie.reindex(index='indicator_name')
但是对于一个系列,正确的语法是什么呢?重新分配索引
ind_serie.index = ind_serie.values
重新分配索引
ind_serie.index = ind_serie.values
您有一个dict of dict,它创建了一个系列,其中一个元素包含第一个dict的列表。这是另一个问题 下面我创建您想要的,从项目列表开始
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
您有一个dict of dict,它创建了一个系列,其中一个元素包含第一个dict的列表。这是另一个问题 下面我创建您想要的,从项目列表开始
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
ind_serie.index=ind_serie.values
工作不正常
ind\u serie.to\u frame.set\u index('indicator\u name',drop=False,inplace=True)
这条路最终会走吗?
ind\u serie.index=ind\u serie.values
无法正常工作
ind\u serie.to\u frame.set\u index('indicator\u name',drop=False,inplace=True)
是最终要走的路我想你指的是系列。@fuzzyhedge:是的……我想你指的是系列。@fuzzyhedge:是的……@fuzzyhedge Thx!然而,我想知道为什么有时候指数会像预期的那样正确,有时候是
(10年期国债拍卖)
(即带括号和逗号?),这可能意味着它被视为自己的形状标准(1,)。在导入过程中可能会有额外的[]。与作为一个元素导入到系列中的dict类似(和相关)问题。我建议查看文档。是的,该系列最初是从mySQL数据库导入的。。如何将索引(或值列)从其自身的形状(1,)数组修改为所需的任何形状,以便在我将括号和逗号用作索引时它们消失?很难说。也许用一个列表替换一个字符串。您应该再问一个问题,并包括更多的输入/输出。@fuzzyhedge Thx!然而,我想知道为什么有时候指数会像预期的那样正确,有时候是(10年期国债拍卖)
(即带括号和逗号?),这可能意味着它被视为自己的形状标准(1,)。在导入过程中可能会有额外的[]。与作为一个元素导入到系列中的dict类似(和相关)问题。我建议查看文档。是的,该系列最初是从mySQL数据库导入的。。如何将索引(或值列)从其自身的形状(1,)数组修改为所需的任何形状,以便在我将括号和逗号用作索引时它们消失?很难说。也许用一个列表替换一个字符串。你应该问另外一个问题,包括更多的输入/输出。@piRSquared Thx!然而,我想知道为什么有时候指数是正确的和预期的,有时候是(10年期国债拍卖)
(即带括号和逗号?)@jimbasquiat,这是因为这是系列值中的内容。可能只有在指数和值的长度相等时才起作用,否则会发生这种情况:ValueError:长度不匹配:预期轴有149个元素,新值有151个元素
@piRSquared Thx!然而,我想知道为什么有时候指数是正确的和预期的,有时候是(10年期国债拍卖)
(即带括号和逗号?)@jimbasquiat,这是因为这是系列值中的内容。可能只有在指数和值的长度相等时才起作用,否则会出现这种情况:ValueError:长度不匹配:预期轴有149个元素,新值有151个元素