Lorenz
Lorenz

Reputation: 306

Pandas: Create new dataframe based on existing dataframe

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

Upvotes: 0

Views: 12574

Answers (2)

FChm
FChm

Reputation: 2590

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

Upvotes: 0

G. Anderson
G. Anderson

Reputation: 5955

You can select and rename the columns in one line

df2=df[['B','D','F']].rename({'B':'X','D':'Y','F':'Z'}, axis=1)

Upvotes: 5

Related Questions