5

I would like to change the column names of a pd dataframe but I'm finding that the order of the columns change after data is retrieved. The below code specifies sector ETF symbols and fetches the data from yahoo finance.

The problem is that once I run the code, for example, 'XLY' is no longer the first series in the dataframe, so I cannot just run sec_perf.columns = ['Name1', 'Name2', etc] as I normally would because it will not name the columns properly. What am I messing up here?

import pandas as pd
import pandas_datareader.data as web
import datetime as datetime

end = datetime.date.today()
secs = ['XLY', 'XLP', 'XLE', 
       'XLF', 'XLV', 'XLI', 
       'XLB', 'XLRE', 'XLK', 'XLU']

sec_perf = web.DataReader(secs, 'yahoo', 
           start = datetime.datetime(2016,12,31), 
           end = end)['Adj Close']
Merv Merzoug
  • 1,149
  • 2
  • 19
  • 33

3 Answers3

4

use reindex_axis

sec_perf.reindex_axis(secs, 1)

enter image description here

You could also have used sec_perf[secs] to do the same thing. But we did this a while ago and determined that reindex_axis was quickest.

enter image description here

Community
  • 1
  • 1
piRSquared
  • 285,575
  • 57
  • 475
  • 624
4

Solution with reindex:

print (sec_perf.reindex(columns=secs))
                  XLY        XLP        XLE        XLF        XLV        XLI  \
Date                                                                           
2017-01-03  81.879997  51.900002  76.169998  23.510000  69.839996  62.590000   
2017-01-04  82.970001  51.900002  76.010002  23.700001  70.389999  62.959999   
2017-01-05  82.910004  52.070000  75.820000  23.459999  70.750000  62.779999   
2017-01-06  83.320000  52.119999  75.889999  23.540001  70.949997  63.139999   
2017-01-09  83.250000  51.700001  74.790001  23.379999  71.250000  62.650002   
2017-01-10  83.550003  51.439999  74.110001  23.430000  71.500000  62.910000   
2017-01-11  83.730003  51.540001  74.910004  23.580000  70.779999  63.240002   
2017-01-12  83.650002  51.490002  74.599998  23.379999  70.849998  62.980000   
2017-01-13  83.959999  51.520000  74.379997  23.510000  70.919998  63.220001   
2017-01-17  84.099998  52.250000  74.839996  22.950001  70.559998  62.730000   
2017-01-18  83.949997  52.430000  74.669998  23.139999  70.470001  62.970001   
2017-01-19  83.690002  52.240002  74.260002  23.040001  70.019997  63.430000   
2017-01-20  83.930000  52.580002  74.540001  23.150000  69.839996  63.439999   
2017-01-23  83.989998  52.560001  73.750000  23.000000  69.550003  63.090000   
2017-01-24  84.680000  52.910000  74.559998  23.290001  69.070000  63.720001   
2017-01-25  85.199997  52.900002  74.949997  23.680000  69.720001  64.389999   
2017-01-26  85.330002  52.669998  75.010002  23.740000  69.180000  64.540001   
2017-01-27  85.059998  52.380001  74.230003  23.650000  69.750000  64.489998   
2017-01-30  84.959999  52.340000  72.870003  23.459999  69.410004  63.939999   

                  XLB       XLRE        XLK        XLU  
Date                                                    
2017-01-03  49.990002  30.850000  48.790001  48.450001  
2017-01-04  50.720001  31.240000  48.959999  48.630001  
2017-01-05  50.570000  31.400000  49.040001  48.680000  
2017-01-06  50.619999  31.400000  49.400002  48.830002  
2017-01-09  50.610001  31.200001  49.389999  48.189999  
2017-01-10  50.639999  30.809999  49.400002  48.040001  
2017-01-11  51.049999  30.639999  49.630001  48.540001  
2017-01-12  50.950001  30.760000  49.509998  48.580002  
2017-01-13  50.869999  30.690001  49.660000  48.509998  
2017-01-17  50.639999  30.940001  49.470001  49.040001  
2017-01-18  50.959999  31.010000  49.599998  48.980000  
2017-01-19  50.639999  30.709999  49.529999  48.549999  
2017-01-20  51.090000  30.900000  49.799999  48.639999  
2017-01-23  51.189999  31.090000  49.889999  48.389999  
2017-01-24  52.509998  31.100000  50.200001  48.380001  
2017-01-25  52.860001  30.910000  50.680000  48.380001  
2017-01-26  53.000000  30.889999  50.540001  48.400002  
2017-01-27  52.810001  30.629999  50.740002  48.389999  
2017-01-30  52.270000  30.459999  50.330002  48.430000  
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
1

You can rename the columns using a dictionary so no matter the position in the dataframe with:

df = df.rename(columns={'oldName1': 'newName1', 'oldName2': 'newName2'})
Fabio Lamanna
  • 20,504
  • 24
  • 90
  • 122