pandas在添加了行后缺少index.name

时间:2017-01-13 07:59:09

标签: python pandas

以下内容对我来说是个错误:

>>> df = pd.DataFrame(np.random.randn(3, 2), columns=['A','B'])
>>> df.index.name = 'some-name'
>>> df
                  A         B
some-name                    
0          0.841645 -0.088688
1          0.779664 -1.263692
2          0.058694  0.414090
>>> df.loc[3] = df.loc[1]
>>> df
          A         B
0  0.841645 -0.088688
1  0.779664 -1.263692
2  0.058694  0.414090
3  0.779664 -1.263692

如果索引标签已存在,则行为应如下:

>>> df = pd.DataFrame(np.random.randn(3, 2), columns=['A','B'])
>>> df.index.name = 'some-name'
>>> df.loc[2] = df.loc[1]
>>> df
                  A         B
some-name                    
0         -0.053362 -0.212953
1          0.300137  0.155986
2          0.300137  0.155986

0 个答案:

没有答案