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
5
  • What have you tried so far? Show us your attempts at this. Commented Mar 12, 2019 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) Commented Mar 12, 2019 at 22:27
  • Welcome to Stack Overflow!! Please review this -- How to create a Minimal, Complete, and Verifiable example -- stackoverflow.com/help/mcve and repost your question. Commented Mar 12, 2019 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. Commented Mar 12, 2019 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']) Commented Mar 12, 2019 at 22:44

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.