Selecting multiple columns in a pandas dataframe

Machine Learning

I have data in different columns but I don't know how to extract it to save it in another variable.

index a b c

1 2 3 4

2 3 4 5

How do I select 'a', 'b' and save it into df1?

I tried

df1 = df['a':'b']

df1 = df.ix[:, 'a':'b']

None seem to work.



You can select multiple column by using column index.



You are allowed to select multiple columns in pandas by using their name. However, please note that the column name gets stored as a list which is referred to as a dictionary. You can use [ [ ] ] in order to pass the selected column names. There is a method df[[‘a’,’b’]] which is capable of producing a copy.

Example : df2 = df [[‘a’,’b’]]

You are also allowed to use the .iloc method which will help in accessing the list by the column based on the index number of a column name.


If you want to unleash your potential in this competitive field, please visit the Machine Learning course page for more information, where you can find the Machine Learning tutorials and Machine Learning frequently asked interview questions and answers as well.


This topic has been locked/unapproved. No replies allowed

Login to participate in this discussion.

Leave a reply

Before proceeding, please check your email for a verification link. If you did not receive the email, click here to request another.