Pandas DataFrame – Select Column

Select Column of Pandas DataFrame

You can select a column from Pandas DataFrame using dot notation or either with brackets.

Syntax

#select column using dot operator
a = myDataframe.column_name
#select column using square brackets
a = myDataframe[coulumn_name]
Run

Selecting a column return Pandas Series.

Example 1: Select a Column using Dot Operator

In this example, we will select a column, from pre-initialized dataframe, using dot operator . . And shall print the column contents and its datatype.

Python Program

import pandas as pd

#initialize dataframe
df = pd.DataFrame({'a': [57, 43, 85], 'b': [92, 30, 66]})

#select column
a = df.a

print('Selected Column\n---------------\n',a,sep='')
print('\n',type(a),sep='')
Run

Output

Selected Column
---------------
0    57
1    43
2    85
Name: a, dtype: int64

<class 'pandas.core.series.Series'>

The selected column is of class type pandas.core.series.Series.

Example 2: Select a column using Square Brackets

In this example, we will select a column from Pandas DataFrame using square brackets [].

Python Program

import pandas as pd

#initialize dataframe
df = pd.DataFrame({'a': [57, 43, 85], 'b': [92, 30, 66]})

#select column
a = df['a']

print('Selected Column\n---------------\n',a,sep='')
print('\n',type(a),sep='')
Run

Output

Selected Column
---------------
0    57
1    43
2    85
Name: a, dtype: int64

<class 'pandas.core.series.Series'>

Selecting a column using square brackets is preferred because in some special scenarios, which we will discuss in the following examples, using dot operator does not work.

Example 3: Select Column whose name has spaces

In this example, we will select column whose name coincides with a function name.

Using dot operator in this scenario returns the column as a method.

Python Program

import pandas as pd

#initialize dataframe
df = pd.DataFrame({'sum': [57, 43, 85], 'b': [92, 30, 66]})

#select column
a = df.sum

print('Selected Column\n---------------\n',a,sep='')
print('\n',type(a),sep='')
Run

Output

Selected Column
---------------
<bound method DataFrame.sum of    sum   b
0   57  92
1   43  30
2   85  66>

<class 'method'>

Using square brackets will select a column and return the Series.

Python Program

import pandas as pd

#initialize dataframe
df = pd.DataFrame({'sum': [57, 43, 85], 'b': [92, 30, 66]})

#select column
a = df['sum']

print('Selected Column\n---------------\n',a,sep='')
print('\n',type(a),sep='')
Run

Output

Selected Column
---------------
0    57
1    43
2    85
Name: sum, dtype: int64

<class 'pandas.core.series.Series'>

Example 4: Select Column Name with Spaces

In this example, we will select column whose name coincides with a function name.

Using dot operator in this scenario throws SyntaxError.

Python Program

import pandas as pd

#initialize dataframe
df = pd.DataFrame({'a': [57, 43, 85], 'b': [92, 30, 66], 'sum a b': [149, 73, 151]})

#select column
a = df.sum a b

print('Selected Column\n---------------\n',a,sep='')
print('\n',type(a),sep='')
Run

Output

  File "example1.py", line 7
    a = df.sum a b
               ^
SyntaxError: invalid syntax

Using square brackets will select the column with spaces and returns Series.

Python Program

import pandas as pd

#initialize dataframe
df = pd.DataFrame({'a': [57, 43, 85], 'b': [92, 30, 66], 'sum a b': [149, 73, 151]})

#select column
a = df['sum a b']

print('Selected Column\n---------------\n',a,sep='')
print('\n',type(a),sep='')
Run

Output

Selected Column
---------------
0    149
1     73
2    151
Name: sum a b, dtype: int64

<class 'pandas.core.series.Series'>

Summary

In this tutorial of Python Examples, we learned how to select a column from Pandas DataFrame with the help of well detailed scenarios.