Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unable to drop the index column and rename the second column

I've the following code and I'm unable to drop the index and rename the seconds column. Both the index and the 2nd column have the same name "Date". How can I fix it?

              Date     Open     High      Low     Last    Close  \
Date                                                                 
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
like image 495
jason Avatar asked Jan 03 '23 14:01

jason


2 Answers

Use rename_axis for change index name, if need column from index add reset_index and if necessary use 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

Or if need remove index and rename:

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

And if need only rename index name and columns name:

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
like image 199
jezrael Avatar answered Jan 11 '23 06:01

jezrael


Set the index to overwrite what's there.

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

To complete your renaming

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
like image 29
piRSquared Avatar answered Jan 11 '23 06:01

piRSquared