无法删除索引列并重命名第二列

时间:2018-04-04 14:16:06

标签: pandas

我有以下代码,我无法删除索引并重命名秒列。索引和第二列都具有相同的名称&#34; GHCi> fmap ($ 3) ((4 :: Integer)+) <interactive>:42:13: error: * Couldn't match type `Integer' with `Integer -> b' Expected type: Integer -> Integer -> b Actual type: Integer -> Integer * In the second argument of `fmap', namely `((4 :: Integer) +)' In the expression: fmap ($ 3) ((4 :: Integer) +) In an equation for `it': it = fmap ($ 3) ((4 :: Integer) +) * Relevant bindings include it :: Integer -> b (bound at <interactive>:42:1) &#34;。我该如何解决?

Date

4 个答案:

答案 0 :(得分:3)

使用rename_axis表示更改索引名称,如果需要index列添加reset_index,必要时使用rename

df = df.rename_axis('Date1').reset_index().rename(columns={'Date':'New'})
print (df)
         Date1         New   Open   High    Low   Last  Close
0   2010-12-31  2010-12-31  71.50  74.00  71.50  72.05  72.40
1   2011-01-31  2011-01-31  70.05  73.00  70.05  72.70  72.75
2   2011-02-28  2011-02-28  77.00  77.00  74.80  74.90  74.85
3   2011-03-31  2011-03-31  54.35  58.85  54.35  55.00  55.25
4   2011-04-30  2011-04-29  60.50  60.50  58.10  58.10  58.20
5   2011-05-31  2011-05-31  52.50  53.50  52.10  52.95  52.55
6   2011-06-30  2011-06-30  46.30  46.95  45.60  46.60  46.65
7   2011-07-31  2011-07-29  49.70  50.40  49.30  49.30  49.60
8   2011-08-31  2011-08-30  45.10  47.90  43.05  47.85  47.45
9   2011-09-30  2011-09-30  42.05  43.45  41.40  41.75  41.85
10  2011-10-31  2011-10-31  43.70  43.70  41.10  43.70  43.70
11  2011-11-30  2011-11-30  39.95  39.95  38.05  38.15  38.15
12  2011-12-31  2011-12-30  31.55  35.80  31.15  34.00  34.10
13  2012-01-31  2012-01-31  44.95  45.30  43.70  44.00  44.00
14  2012-02-29  2012-02-29  47.90  49.00  46.15  47.00  46.25
15  2012-03-31  2012-03-30  44.75  44.75  43.60  44.40  44.45
16  2012-04-30  2012-04-30  70.00  75.60  66.70  75.60  75.60

或者如果需要删除索引并重命名:

df = df.reset_index(drop=True).rename(columns={'Date':'New'})
print (df)
           New   Open   High    Low   Last  Close
0   2010-12-31  71.50  74.00  71.50  72.05  72.40
1   2011-01-31  70.05  73.00  70.05  72.70  72.75
2   2011-02-28  77.00  77.00  74.80  74.90  74.85
3   2011-03-31  54.35  58.85  54.35  55.00  55.25
4   2011-04-29  60.50  60.50  58.10  58.10  58.20
5   2011-05-31  52.50  53.50  52.10  52.95  52.55
6   2011-06-30  46.30  46.95  45.60  46.60  46.65
7   2011-07-29  49.70  50.40  49.30  49.30  49.60
8   2011-08-30  45.10  47.90  43.05  47.85  47.45
9   2011-09-30  42.05  43.45  41.40  41.75  41.85
10  2011-10-31  43.70  43.70  41.10  43.70  43.70
11  2011-11-30  39.95  39.95  38.05  38.15  38.15
12  2011-12-30  31.55  35.80  31.15  34.00  34.10
13  2012-01-31  44.95  45.30  43.70  44.00  44.00
14  2012-02-29  47.90  49.00  46.15  47.00  46.25
15  2012-03-30  44.75  44.75  43.60  44.40  44.45
16  2012-04-30  70.00  75.60  66.70  75.60  75.60

如果只需要重命名索引名称和列名称:

df = df.rename_axis('Date1').rename(columns={'Date':'New'})
print (df)
                   New   Open   High    Low   Last  Close
Date1                                                    
2010-12-31  2010-12-31  71.50  74.00  71.50  72.05  72.40
2011-01-31  2011-01-31  70.05  73.00  70.05  72.70  72.75
2011-02-28  2011-02-28  77.00  77.00  74.80  74.90  74.85
2011-03-31  2011-03-31  54.35  58.85  54.35  55.00  55.25
2011-04-30  2011-04-29  60.50  60.50  58.10  58.10  58.20
2011-05-31  2011-05-31  52.50  53.50  52.10  52.95  52.55
2011-06-30  2011-06-30  46.30  46.95  45.60  46.60  46.65
2011-07-31  2011-07-29  49.70  50.40  49.30  49.30  49.60
2011-08-31  2011-08-30  45.10  47.90  43.05  47.85  47.45
2011-09-30  2011-09-30  42.05  43.45  41.40  41.75  41.85
2011-10-31  2011-10-31  43.70  43.70  41.10  43.70  43.70
2011-11-30  2011-11-30  39.95  39.95  38.05  38.15  38.15
2011-12-31  2011-12-30  31.55  35.80  31.15  34.00  34.10
2012-01-31  2012-01-31  44.95  45.30  43.70  44.00  44.00
2012-02-29  2012-02-29  47.90  49.00  46.15  47.00  46.25
2012-03-31  2012-03-30  44.75  44.75  43.60  44.40  44.45
2012-04-30  2012-04-30  70.00  75.60  66.70  75.60  75.60

答案 1 :(得分:2)

设置索引以覆盖那里的内容。

df.set_index('Date')

             Open   High    Low   Last  Close
Date                                         
2010-12-31  71.50  74.00  71.50  72.05  72.40
2011-01-31  70.05  73.00  70.05  72.70  72.75
2011-02-28  77.00  77.00  74.80  74.90  74.85
2011-03-31  54.35  58.85  54.35  55.00  55.25
2011-04-29  60.50  60.50  58.10  58.10  58.20
2011-05-31  52.50  53.50  52.10  52.95  52.55
2011-06-30  46.30  46.95  45.60  46.60  46.65
2011-07-29  49.70  50.40  49.30  49.30  49.60
2011-08-30  45.10  47.90  43.05  47.85  47.45
2011-09-30  42.05  43.45  41.40  41.75  41.85
2011-10-31  43.70  43.70  41.10  43.70  43.70
2011-11-30  39.95  39.95  38.05  38.15  38.15
2011-12-30  31.55  35.80  31.15  34.00  34.10
2012-01-31  44.95  45.30  43.70  44.00  44.00
2012-02-29  47.90  49.00  46.15  47.00  46.25
2012-03-30  44.75  44.75  43.60  44.40  44.45
2012-04-30  70.00  75.60  66.70  75.60  75.60

完成重命名

df.set_index('Date').rename_axis('SomeName').reset_index()

      SomeName   Open   High    Low   Last  Close
0   2010-12-31  71.50  74.00  71.50  72.05  72.40
1   2011-01-31  70.05  73.00  70.05  72.70  72.75
2   2011-02-28  77.00  77.00  74.80  74.90  74.85
3   2011-03-31  54.35  58.85  54.35  55.00  55.25
4   2011-04-29  60.50  60.50  58.10  58.10  58.20
5   2011-05-31  52.50  53.50  52.10  52.95  52.55
6   2011-06-30  46.30  46.95  45.60  46.60  46.65
7   2011-07-29  49.70  50.40  49.30  49.30  49.60
8   2011-08-30  45.10  47.90  43.05  47.85  47.45
9   2011-09-30  42.05  43.45  41.40  41.75  41.85
10  2011-10-31  43.70  43.70  41.10  43.70  43.70
11  2011-11-30  39.95  39.95  38.05  38.15  38.15
12  2011-12-30  31.55  35.80  31.15  34.00  34.10
13  2012-01-31  44.95  45.30  43.70  44.00  44.00
14  2012-02-29  47.90  49.00  46.15  47.00  46.25
15  2012-03-30  44.75  44.75  43.60  44.40  44.45
16  2012-04-30  70.00  75.60  66.70  75.60  75.60

答案 2 :(得分:2)

使用rename_axis,您有不同的名称,可以添加reset_index()

df.rename_axis('Yourname')

df.index.name='Yourname'

答案 3 :(得分:1)

尝试使用重置索引:

df.reset_index(drop = True)