0

what is the most elegant way to create a new dataframe from an existing dataframe, by 1. selecting only certain columns and 2. renaming them at the same time?

For instance I have the following dataframe, where I want to pick column B, D and F and rename them into X, Y, Z

base dataframe

A B C D E F
1 2 3 4 5 6
1 2 3 4 5 6

new dataframe

X Y Z
2 4 6
2 4 6
Lorenz
  • 266
  • 1
  • 5
  • 16
  • What have you tried so far? Show us your attempts at this. – run-out Mar 12 '19 at 22:25
  • You can use a dictionary with `df.rename()` to rename a certain subset of columns and then simultaneously index with the keys. So `d = {"B": "X", "D": "Y", "F": "Z"}` and then simply do something like `new = df[list(d.keys())].rename(columns=d)` – alkasm Mar 12 '19 at 22:27
  • Welcome to Stack Overflow!! Please review this -- How to create a Minimal, Complete, and Verifiable example -- https://stackoverflow.com/help/mcve and repost your question. – Life is complex Mar 12 '19 at 22:29
  • Thanks all for your answers - so it looks like i need to have two separate steps: 1. selection and 2. renaming. I was hoping to have something elegant like when i define a dict. – Lorenz Mar 12 '19 at 22:40
  • I tried something like this - but this looks not natural df = pd.concat( [ df['B'], df['D'], df['F'], ], axis=1, keys=['x','y','z']) – Lorenz Mar 12 '19 at 22:44

2 Answers2

5

You can select and rename the columns in one line

df2=df[['B','D','F']].rename({'B':'X','D':'Y','F':'Z'}, axis=1)
G. Anderson
  • 5,815
  • 2
  • 14
  • 21
0

Slightly more general selection of every other column:

df = pd.DataFrame({'A':[1,2,3], 'B':[4,5,6], 
                   'C':[7,8,9], 'D':[10,11,12]})

df_half = df.iloc[:, ::2]

with df_half being:

    A   C
0   1   7
1   2   8
2   3   9

You can then use the rename method mentioned in the answer by @G. Anderson or directly assign to the columns:

df_half.columns = ['X','Y']

returning:

    X   Y
0   1   7
1   2   8
2   3   9
FChm
  • 2,515
  • 1
  • 17
  • 37