简体   繁体   中英

How to select n-1 column in a dataframe

We need to select nl column from n column in a Dataframe using python we have tried this method

Sample code

X = dataframe1[['Col1,Col2......n']]

Is there any other way around

If you have a pandas DataFrame df with column names ['Col1', 'Col2', ...] you can do something like:

df.ix[:,'Col2':] # to select Col2, Col3, ...

or

df.ix[:,:'Col4'] # to select Col1, Col2, Col3 and Col4

For recent queries, ix is deprecated:

df.loc[:, :"col[n-1]"] # To select columns except the last one. This is label based.

For index-based:

df.iloc[:, :(n - 1)] # To select columns except the last one. This is index based.

In general:

df.iloc[startRowNumber: endRowNumber, startColumnNumber: endColumnNumber]

Yes,the solution is

df.ix[:,columnname1:columname2]

columnname1--> it is the first column you want to include columnname2-->it is the last column where you want to select

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM