Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select Multiple Columns in DataFrame Pandas. Slice + Select

I have de DataFrame with almost 100 columns

I need to select col2 to col4 and col54. How can I do it? I tried:

df = df.loc[:,'col2':col4'] 

but i can't add col54

like image 477
ramiro rodriguez Avatar asked Oct 27 '25 04:10

ramiro rodriguez


1 Answers

You can do this in a couple of different ways:

Using the same format you are currently trying to use, I think doing a join of col54 will be necessary.

df = df.loc[:,'col2':'col4'].join(df.loc[:,'col54'])

.

Another method given that col2 is close to col4 would be to do this

df = df.loc[:,['col2','col3','col4', 'col54']]

or simply

df = df[['col2','col3','col4','col54']]
like image 167
Keith Galli Avatar answered Oct 29 '25 18:10

Keith Galli