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]

Selecting a column return Pandas Series.

Examples

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 with name 'a'
a = df.a

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

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.

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 with name 'a'
a = df['a']

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

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.

3. 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 Code Copy

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 with name 'sum a b'
a = df['sum a b']

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

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.

Related Tutorials

Code copied to clipboard successfully 👍