Get a list of a Particular Column Values of a Pandas Dataframe

A Pandas DataFrame is a two-dimensional data structure similar to spreadsheets or SQL tables. Often, you need to extract values from a specific column as a Python list for further processing or analysis.

There are several methods to extract column values from a DataFrame ?

  • Using .values.tolist() method

  • Using .loc[] method

  • Using .iloc[] method

  • Using get() function

Using .values.tolist() Method

The .values attribute extracts the underlying NumPy array, and .tolist() converts it to a Python list.

Syntax

col_vals = df['col_name'].values.tolist()

Example

Extract the values from the "Favourite Subject" column using the .values.tolist() method ?

import pandas as pd

# Create student data
student_data = {
    'Name': ['Alice', 'Cassie', 'Henry', 'Steven'],
    'Age': [15, 13, 16, 14],
    'Favourite Subject': ['Math', 'Social', 'Science', 'English']
}

# Create DataFrame
df = pd.DataFrame(student_data)
print("DataFrame:")
print(df)

# Extract column values as list
col_vals = df['Favourite Subject'].values.tolist()
print("\nExtracted column values:")
print(col_vals)
DataFrame:
    Name  Age Favourite Subject
0  Alice   15              Math
1 Cassie   13            Social
2  Henry   16           Science
3 Steven   14           English

Extracted column values:
['Math', 'Social', 'Science', 'English']

Using .loc[] Method

The .loc[] method selects rows and columns by labels. Use : to select all rows and specify the column name.

Syntax

col_vals = df.loc[:, 'col_name'].tolist()

Example

import pandas as pd

# Create student data
student_data = {
    'Name': ['Alice', 'Cassie', 'Henry', 'Steven'],
    'Age': [15, 13, 16, 14],
    'Favourite Subject': ['Math', 'Social', 'Science', 'English']
}

# Create DataFrame
df = pd.DataFrame(student_data)

# Extract column values using loc
col_vals = df.loc[:, 'Favourite Subject'].tolist()
print("Extracted column values:")
print(col_vals)
Extracted column values:
['Math', 'Social', 'Science', 'English']

Using .iloc[] Method

The .iloc[] method selects rows and columns by integer positions. Column indices start from 0.

Syntax

col_vals = df.iloc[:, col_index].tolist()

Example

Extract values from the third column (index 2) using .iloc[] ?

import pandas as pd

# Create student data
student_data = {
    'Name': ['Alice', 'Cassie', 'Henry', 'Steven'],
    'Age': [15, 13, 16, 14],
    'Favourite Subject': ['Math', 'Social', 'Science', 'English']
}

# Create DataFrame
df = pd.DataFrame(student_data)

# Extract column values using iloc (index 2 = third column)
col_vals = df.iloc[:, 2].tolist()
print("Extracted column values:")
print(col_vals)
Extracted column values:
['Math', 'Social', 'Science', 'English']

Using get() Function

The get() function safely retrieves a column by name, returning None if the column doesn't exist.

Syntax

col_vals = df.get('col_name').tolist()

Example

import pandas as pd

# Create student data
student_data = {
    'Name': ['Alice', 'Cassie', 'Henry', 'Steven'],
    'Age': [15, 13, 16, 14],
    'Favourite Subject': ['Math', 'Social', 'Science', 'English']
}

# Create DataFrame
df = pd.DataFrame(student_data)

# Extract column values using get()
col_vals = df.get('Favourite Subject').tolist()
print("Extracted column values:")
print(col_vals)
Extracted column values:
['Math', 'Social', 'Science', 'English']

Comparison

Method Access Type Best For
.values.tolist() Direct column access Simple column extraction
.loc[] Label-based Complex row/column selection
.iloc[] Position-based Index-based selection
get() Safe column access When column may not exist

Conclusion

Use .values.tolist() for simple column extraction. Use get() when the column might not exist. Use .loc[] and .iloc[] for more complex data selection scenarios.

Updated on: 2026-03-27T12:06:24+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements