Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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()methodUsing
.loc[]methodUsing
.iloc[]methodUsing
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.
