13

I'm construction a new DataFrame by concatenating the columns of other DataFrames, like so:

pairs = pd.concat([pos1['Close'], pos2['Close'], pos3['Close'], pos4['Close'], pos5['Close'],
                  pos6['Close'], pos7['Close']], axis=1)

I want to rename all of the columns of the pairs Dataframe to the symbol of the underlying securities. Is there a way to do this during the the concat method call? Reading through the docs on the method here http://pandas.pydata.org/pandas-docs/version/0.17.0/generated/pandas.concat.html didn't give me a solid answer.

Ignacio Alorre
  • 6,715
  • 8
  • 54
  • 83
Thomas Murphy
  • 1,152
  • 4
  • 13
  • 39
  • You'd need to do this as a post processing step or a chained call – EdChum Dec 12 '16 at 15:09
  • @EdChum So set up a list of strings and do an enumerate calling: pairs.columns.values[idx] = symbol ? – Thomas Murphy Dec 12 '16 at 15:19
  • You should use `rename(columns=some_dict)` or if you know the final order then make a list comprehension and call `pairs.columns = your_list` also you can use `rename_axis` – EdChum Dec 12 '16 at 15:20
  • see this: http://stackoverflow.com/questions/11346283/renaming-columns-in-pandas?s=1|6.3872 – EdChum Dec 12 '16 at 15:21
  • The rename approach doesn't work because the value for all columns is 'Close', so any value passed in renames all columns simultaneously…so there has to be some use of idx. – Thomas Murphy Dec 12 '16 at 15:24

2 Answers2

9

This is the approach I'm taking. Seems to fit all my requirements.

symbols = ['JPM', 'WFC', 'BAC', 'C', 'STI', 'PNC', 'CMA']

pairs.columns = symbols
Thomas Murphy
  • 1,152
  • 4
  • 13
  • 39
  • 1
    You can just do `pairs.columns = symbols`, assigning directly to the underlying numpy array representation may not always work – EdChum Dec 12 '16 at 15:25
  • @EdChum Can you give an example of when it won't work? Would be good to have a complete discussion on this thread. – Thomas Murphy Dec 13 '16 at 15:42
  • The problem here is that `.values` returns you a np array of the underlying values, here we enter into unknown territory as we don't know if you're returned a view or a copy of the data. In general one should always use the provided methods for these kind of operations to avoid this ambiguity – EdChum Dec 13 '16 at 15:57
  • @EdChum Edited to reflect that feedback – Thomas Murphy Dec 13 '16 at 18:46
8

You can achieve the same in one go using the attribute keys:

pairs = pd.concat(
    [pos1['Close'],
     pos2['Close'],
     pos3['Close'],
     pos4['Close'],
     pos5['Close'],
     pos6['Close'],
     pos7['Close']
    ], 
    axis=1,
    keys=['JPM',
          'WFC',
          'BAC',
          'C',
          'STI',
          'PNC',
          'CMA'
         ]
)
yoonghm
  • 3,320
  • 1
  • 27
  • 43
Ignacio Alorre
  • 6,715
  • 8
  • 54
  • 83