Select columns pandas

Select columns pandas DEFAULT

Interesting Ways to Select Pandas DataFrame Columns

Example Data

If you want to use the data I used to test out these methods of selecting columns from a pandas data frame, use the code snippet below to get the wine dataset into your IDE or a notebook.

from sklearn.datasets import load_wine
import pandas as pd
import numpy as np
import reX = load_wine()
df = pd.DataFrame(, columns = X.feature_names)df.head()
Image of Table of Wine Data
Image of Table of Wine Data

Now, depending on what you want to do, check out each one of the code snippets below and try for yourself!

Selecting columns based on their name

This is the most basic way to select a single column from a dataframe, just put the string name of the column in brackets. Returns a pandas series.


Passing a list in the brackets lets you select multiple columns at the same time.


Selecting a subset of columns found in a list

Similar to the previous example, but here you can search over all the columns in the dataframe.

df[df.columns[df.columns.isin(['alcohol','hue','NON-EXISTANT COLUMN'])]]

Selecting a subset of columns based on difference of columns

Let’s say you know what columns you don’t want in the dataframe. Pass those as a list to the difference method and you’ll get back everything except them.


Selecting a subset of columns that is not in a list

Return a data frame that has columns that are not in a list that you want to search over.


Selecting columns based on their data type

Data types include ‘float64’ and ‘object’ and are inferred from the columns passed to the dtypes method. By matching on columns that are the same data type, you’ll get a series of True/False. Use the values method to get just the True/False values and not the index.


Selecting columns based on their column name containing a substring

If you have tons of columns in a data frame and their column names all have a similar substring that you are interested in, you can return the columns who’s names contain a substring. Here we want everything that has the “al” substring in it.

df.loc[:,['al' in i for i in df.columns]]

Selecting columns based on their column name containing a string wildcard

You could have hundreds of columns, so it might make sense to find columns that match a pattern. Searching for column names that match a wildcard can be done with the “search” function from the re package (see the link in the reference section for more details on using the regular expression package).

df.loc[:,[True if'flava+',column) else False for column in df.columns]]

Selecting columns based on how their column name starts

If you want to select columns with names that start with a certain string, you can use the startswith method and pass it in the columns spot for the data frame location.


Selecting columns based on how their column name ends

Same as the last example, but finds columns with names that end a certain way.


Selecting columns if all rows meet a condition

You can pick columns if the rows meet a condition. Here, if all the the values in a column is greater than 14, we return the column from the data frame.

df.loc[:,[(df[col] > 14).all() for col in df.columns]]

Selecting columns if any row of a column meets a condition

Here, if any of the the values in a column is greater than 14, we return the column from the data frame.

df.loc[:,[(df[col] > 14).any() for col in df.columns]]

Selecting columns if the average of rows in a column meet a condition

Here, if the mean of all the values in a column meet a condition, return the column.

df.loc[:,[(df[col].mean() > 7) for col in df.columns]]

This article explores all the different ways you can use to select columns in Pandas, including using loc, iloc, and how to create copies of dataframes. You’ll learn a ton of different tricks for selecting columns using handy follow along examples.

Let’s get started!

Why Select Columns in Python?

The data you work with in lots of tutorials has very clean data with a limited number of columns. But this isn’t true all the time.

In many cases, you’ll run into datasets that have many columns – most of which are not needed for your analysis.

In this case, you’ll want to select out a number of columns.

This often has the added benefit of using less memory on your computer (when removing columns you don’t need), as well as reducing the amount of columns you need to keep track of mentally.

Select columns in Pandas with loc, iloc, and the indexing operator! Click to Tweet

Creating our Dataframe

To get started, let’s create our dataframe to use throughout this tutorial. We’ll create one that has multiple columns, but a small amount of data (to be able to print the whole thing more easily).

We’ll need to import pandas and create some data. Simply copy the code and paste it into your editor or notebook.

import pandas as pd df = pd.read_csv('') print(df.head())

This returns the following:

Let’s take a quick look at what makes up a dataframe in Pandas:

What is Pandas Dataframe

Using loc to Select Columns

The loc function is a great way to select a single column or multiple columns in a dataframe if you know the column name(s).

This method is great for:

  • Selecting columns by column name,
  • Selecting rows along columns,
  • Selecting columns using a single label, a list of labels, or a slice

The loc method looks like this:

Pandas Select Columns with loc

Now, if you wanted to select only the name column and the first three rows, you would write:

selection = df.loc[:2,'Name'] print(selection)

This returns:

You’ll probably notice that this didn’t return the column header.

Note: Indexes in Pandas start at 0. That means if you wanted to select the first item, we would use position 0, not 1.

If you wanted to select multiple columns, you can include their names in a list:

selection = df.loc[:2,['Name', 'Age', 'Height', 'Score']] print(selection)

This returns:

Additionally, you can slice columns if you want to return those columns as well as those in between. The same code we wrote above, can be re-written like this:

selection = df.loc[:2,'Name':'Score'] print(selection)

This returns:

Now, let’s take a look at the iloc method for selecting columns in Pandas.

Using iloc to Select Columns

The iloc function is one of the primary way of selecting data in Pandas. The method “iloc” stands for integer location indexing, where rows and columns are selected using their integer positions.

This method is great for:

  • Selecting columns by column position (index),
  • Selecting rows along with columns,
  • Selecting columns using a single position, a list of positions, or a slice of positions

The standard format of the iloc method looks like this:

Pandas Select Columns with iloc

Now, for example, if we wanted to select the first two rows and first three columns of our dataframe, we could write:

selection = df.iloc[:2,:2] print(selection)

This returns:

Note that we didn’t write df.iloc[0:2,0:2], but that would have yielded the same result.

If we wanted to select all columns with iloc, we could do that by writing:

selection = df.iloc[:2,] print(selection)

This returns:

Similarly, we could select all rows by leaving out the first values (but including a colon before the comma).

selection = df.iloc[:,:2] print(selection)

This returns:

Select a Single Column in Pandas

Now, if you want to select just a single column, there’s a much easier way than using either loc or iloc.

This can be done by selecting the column as a series in Pandas. You can pass the column name as a string to the indexing operator.

For example, to select only the Name column, you can write:

selection = df['Name'] print(selection)

Doing this, this returns the following:

Similarly, you can select columns by using the dot operator. To do the same as above using the dot operator, you could write:

selection = df.Name print(selection)

This returns the same as above:

However, using the dot operator is often not recommended (while it’s easier to type). This is because you can’t:

  1. Select columns with spaces in the name,
  2. Use columns that have the same names as dataframe methods (such as ‘type’),
  3. Pick columns that aren’t strings, and
  4. Select multiple columns.

Check out some other Python tutorials on datagy, including our complete guide to styling Pandas and our comprehensive overview of Pivot Tables in Pandas!

Select Multiple Columns in Pandas

Similar to the code you wrote above, you can select multiple columns.

To do this, simply wrap the column names in double square brackets.

If you wanted to select the Name, Age, and Height columns, you would write:

selection = df[['Name', 'Age', 'Height']] print(selection)

This returns:

What’s great about this method, is that you can return columns in whatever order you want. If you wanted to switch the order around, you could just change it in your list:

selection = df[['Name', 'Height', 'Age']] print(selection)

Which returns:

Copying Columns vs. Selecting Columns

Something important to note for all the methods covered above, it might looks like fresh dataframes were created for each. However, that’s not the case!

In Python, the equal sign (“=”), creates a reference to that object.

Because of this, you’ll run into issues when trying to modify a copied dataframe.

In order to avoid this, you’ll want to use the .copy() method to create a brand new object, that isn’t just a reference to the original.

To accomplish this, simply append .copy() to the end of your assignment to create the new dataframe.

For example, if we wanted to create a filtered dataframe of our original that only includes the first four columns, we could write:

new_df = df.iloc[:,:4].copy() print(new_df)

This results in this code below:

This is incredibly helpful if you want to work the only a smaller subset of a dataframe.

Conclusion: Using Pandas to Select Columns

Thanks for reading all the way to end of this tutorial!

Using follow-along examples, you learned how to select columns using the loc method (to select based on names), the iloc method (to select based on column/row numbers), and, finally, how to create copies of your dataframes.

You also learned how to make column selection easier, when you want to select all rows.


  1. White corsair ram
  2. Travis county mugshots 2015
  3. Patient first nurse salary
  4. Led strip 5630

How to select multiple columns in a pandas dataframe

Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.

Let’s discuss all different ways of selecting multiple columns in a pandas DataFrame.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

Method #1: Basic Method

Given a dictionary which contains Employee entity as keys and list of those entity as values.





Select Second to fourth column.






Method #2: Using

Example 1: Select two columns





Example 2: Select one to another columns. In our case we select column name “Name” to “Address”.





Example 3: First filtering rows and selecting columns by label format and then Select all columns.





Method #3: Using

Example 1: Select first two column.





Example 2: Select all or some columns, one to another using .iloc.






Method #4: Using

Select all or some columns, one to another using .






How To Select One or More Columns in Pandas?

How To Select Columns in Python Pandas?

Selecting a column or multiple columns from a Pandas dataframe is a common task in exploratory data analysis in doing data science/munging/wrangling.

In this post, we will see examples of

  • How to select one column from Pandas dataframe?
  • How to select multiple columns from Pandas dataframe?

Let us first load Pandas library

import pandas as pd

Let us use gapminder dataset from Carpentries website to select columns.

data_url = '' gapminder = pd.read_csv(data_url) gapminder.head(n=3)

We can see that gapminder data frame has six columns or variables.

country year pop continent lifeExp gdpPercap 0 Afghanistan 1952 8425333.0 Asia 28.801 779.445314 1 Afghanistan 1957 9240934.0 Asia 30.332 820.853030 2 Afghanistan 1962 10267083.0 Asia 31.997 853.100710

How to Select One Column from Dataframe in Pandas?

The easiest way to select a column from a dataframe in Pandas is to use name of the column of interest. For example, to select column with the name “continent” as argument []

gapminder['continent'] 0 Asia 1 Asia 2 Asia 3 Asia 4 Asia

Directly specifying the column name to [] like above returns a Pandas Series object. We can see that using type function on the returned object.

>type(gapminder['continent']) pandas.core.series.Series

If we want to select a single column and want a DataFrame containing just the single column, we need to use [[]], double square bracket with a single column name inside it. For example, to select the continent column and get a Pandas data frame with single column as output

>gapminder[['continent']]) continent 0 Asia 1 Asia 2 Asia 3 Asia 4 Asia

Note that now the result has column name “continent” hinting that we now have a dataframe. We can check that using type function as before.

>type(gapminder[['continent']]) pandas.core.frame.DataFrame

How to Select Multiple Columns from a Data Frame in Pandas?

We can use double square brackets [[]] to select multiple columns from a data frame in Pandas. In the above example, we used a list containing just a single variable/column name to select the column. If we want to select multiple columns, we specify the list of column names in the order we like.

For example, to select two columns “country” and “year”, we use the [[]] with two column names inside.

# select multiple columns using column names as list gapminder[['country','year']].head() country year 0 Afghanistan 1952 1 Afghanistan 1957 2 Afghanistan 1962 3 Afghanistan 1967 4 Afghanistan 1972

Selecting Multiple Columns in Pandas Using loc

We can also use “loc” function to select multiple columns. For example, to select the two columns [‘country’,’year’], we can use

# select multiple columns using loc gapminder.loc[,: ['country','year']].head() country year 0 Afghanistan 1952 1 Afghanistan 1957 2 Afghanistan 1962 3 Afghanistan 1967 4 Afghanistan 1972

How to Select Multiple Columns Using Column Index in Pandas?

Sometimes, it is easier to select columns by their location instead of the column names.

We can get the columns of a data frame using columns function

# get column names of Pandas dataframe >gapminder.columns Index(['country', 'year', 'pop', 'continent', 'lifeExp', 'gdpPercap'], dtype='object')

Selecting first N columns in Pandas

To select the first two or N columns we can use the column index slice “gapminder.columns[0:2]” and get the first two columns of Pandas dataframe.

# select first two columns gapminder[gapminder.columns[0:2]].head() country year 0 Afghanistan 1952 1 Afghanistan 1957 2 Afghanistan 1962 3 Afghanistan 1967 4 Afghanistan 1972

Selecting last N columns in Pandas

One of the advantages of using column index slice to select columns from Pandas dataframe is that we can get part of the data frame. For example, to select the last two (or N) columns, we can use column index of last two columns
“gapminder.columns[-2:gapminder.columns.size]” and select them as before.

# gapminder.columns.size gets the number of columns # gapminder.columns[-2:gapminder.columns.size] gets the last two columns gapminder[gapminder.columns[-2:gapminder.columns.size]] lifeExp gdpPercap 0 28.801 779.445314 1 30.332 820.853030 2 31.997 853.100710 3 34.020 836.197138 4 36.088 739.981106

Filed Under: Pandas Select a Column, Pandas Select Multiple Columns, Python, Python Tips, Select Columns PandasTagged With: Pandas Select a Column, Pandas Select Multiple Columns, Select Columns Pandas


Columns pandas select

And what we were doing there. Mikhail calmly and confidently looked into the guy's face and with a confident movement extended his open. Hand to shake. Alexei relaxed and responded to a firm grip.

Python Pandas: Select, SLICE \u0026 FILTER Data rows \u0026 columns by Index or Conditionals

He was so excited for a long time, being on the edge, and constantly holding himself that ejaculation happened on his own. Did not bring him the usual pleasure. It was like the execution of the physiological need associated with the removal of excess fluid from the body. There was a buzzing in his knees, and Samir sat down tiredly on the toilet.

Similar news:

I drove a stake into her to the very balls, pulled out to the middle of the head and again plunged it. Into her flesh. I bared her boobs with my hands and began to knead and caress them.

35542 35543 35544 35545 35546