Python pandas is a powerful library that allows you to easily transform and analyze data in tabular format in python. It stores data as dataframes which are similar to tables, with rows, columns and functions. Often you may need to select multiple columns in Pandas dataframe. In this article, we will learn how to do this. This is a common problem faced by beginners while using Pandas.
How to Select Multiple Columns in Pandas Dataframe
Let us say you have the following pandas dataframe.
index a b c 1 2 3 4 2 3 4 5
Let us say you want to select columns a and b from the above dataframe. Here is a simple command to do this.
df1 = df[['a', 'b']]
The above method is useful if your columns are named or if you know the names of your columns. Just change the column names mentioned in quotes in [].
Alternately, if you don’t know the names of your columns then you can select them using indexes.
df1 = df.iloc[:, 0:2]
Another thing to keep in mind is the difference between a copy of object vs view of Pandas object. The first method creates a copy of the dataframe while the second one returns a view. Depending on your requirement, you can use either of these methods.
Nevertheless, avoid using the following syntax as they don’t work.
df1 = df['a':'b'] df1 = df.ix[:, 'a':'b']
In this article, we have learnt how to select multiple columns in pandas.
Also read:
How to Sort Python List By Multiple Elements
How to Remove Duplicates in Python Pandas
How to Check if String is Integer in Python
How to Shuffle List of Objects in Python
How to Find Local IP Address in Python
Related posts:
How to Repeat String N Times in Python
How to Check if Key Exists in Python Dictionary
How to Merge Folders & Directories in Python
How to Permanently Add Directory to Python Path
How to Import CSV in Python
How to Import from Another Folder in Python
How to Group by Multiple Columns in Python Pandas
Python Script to Load Data in MySQL
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.