22

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.

5
  • You'd need to do this as a post processing step or a chained call
    – EdChum
    Commented Dec 12, 2016 at 15:09
  • @EdChum So set up a list of strings and do an enumerate calling: pairs.columns.values[idx] = symbol ? Commented Dec 12, 2016 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
    Commented Dec 12, 2016 at 15:20
  • see this: stackoverflow.com/questions/11346283/…
    – EdChum
    Commented Dec 12, 2016 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. Commented Dec 12, 2016 at 15:24

2 Answers 2

22

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'])
1
  • 3
    It just add a new level to my columns in my case.
    – huang
    Commented Aug 29, 2022 at 20:47
13

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

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

pairs.columns = symbols
4
  • 1
    You can just do pairs.columns = symbols, assigning directly to the underlying numpy array representation may not always work
    – EdChum
    Commented Dec 12, 2016 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. Commented Dec 13, 2016 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
    Commented Dec 13, 2016 at 15:57
  • @EdChum Edited to reflect that feedback Commented Dec 13, 2016 at 18:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.