SteveS
SteveS

Reputation: 4040

How to slice a dataframe with specific column names + range of columns?

Given any pandas dataframe. I want to select columns A, B and F:Z

I have tried to do it df.loc[:, ['A','B','F':'Z']] but it didn't work. Please advise how to do this.

Upvotes: 0

Views: 1034

Answers (1)

BENY
BENY

Reputation: 323226

We can do two slice then combine

df.loc[:, ['A','B']].join(df.loc[:,'F':'Z'])

And side solution change all name to position then we can do

df.iloc[:,np.r_[1,2,5:999]]

Upvotes: 4

Related Questions