In this article, we’ll see how to get all values of a column in a pandas dataframe in the form of a list. This can be very useful in many situations, suppose we have to get the marks of all the students in a particular subject, get the phone numbers of all the employees, etc. Let’s see how we can achieve this with the help of some examples.
Pandas Get a List of Particular Column Values Below are the ways by which we can get a list of particular column values:
Using tolist() Using get() Using .loc[] Example 1: Get a List of a Particular Column Using tolist() Method In this example, a Pandas DataFrame is created from a dictionary, containing ‘Name’ and ‘Marks’ columns. The values of the ‘Marks’ column are extracted into a Python list using tolist() .
Python3
import pandas as pd
dict = {'Name': ['Martha', 'Tim',
'Rob', 'Georgia'],
'Marks': [87, 91,
97, 95]}
df = pd.DataFrame(dict)
print(df)
marks_list = df['Marks'].tolist()
print(marks_list)
Output:
Name Marks
0 Martha 87
1 Tim 91
2 Rob 97
3 Georgia 95
[87, 91, 97, 95]
Example: Iterate over Columns of a Pandas Dataframe
In this example, a Pandas DataFrame is created from a dictionary with ‘Name’ and ‘Marks’ columns. The code iterates through each column, and for each column, it prints the list of values obtained by applying the tolist() method.
Python3
import pandas as pd
dict = {'Name': ['Martha', 'Tim',
'Rob', 'Georgia'],
'Marks': [87, 91,
97, 95]}
df = pd.DataFrame(dict)
print(df)
for i in list(df):
print(df[i].tolist())
Output:
Name Marks
0 Martha 87
1 Tim 91
2 Rob 97
3 Georgia 95
['Martha', 'Tim', 'Rob', 'Georgia']
[87, 91, 97, 95]
Example 2: Pandas Get a List of a Particular Column Value Using get() Method In this example, a Pandas DataFrame is formed from a dictionary, and the code uses the get() method to extract the ‘Marks’ column, converting it into a Python list with the tolist() method, followed by printing the resulting list.
Python3
import pandas as pd
dict = {'Name': ['Martha', 'Tim',
'Rob', 'Georgia'],
'Marks': [87, 91,
97, 95]}
df = pd.DataFrame(dict)
print(df)
marks_column = df.get('Marks')
marks_list_using_get = marks_column.tolist()
print(marks_list_using_get)
Output
Name Marks
0 Martha 87
1 Tim 91
2 Rob 97
3 Georgia 95
[87, 91, 97, 95]
Example 3: Python Pandas Get a List of Particular Column Values Using .loc[] Method In this example, a Pandas DataFrame is created from a dictionary with ‘Name’ and ‘Marks’ columns. The code utilizes the .loc[] method to extract and print the list of values from the ‘Marks’ column.
Python3
import pandas as pd
dict = {'Name': ['Martha', 'Tim',
'Rob', 'Georgia'],
'Marks': [87, 91,
97, 95]}
df = pd.DataFrame(dict)
print(df)
marks_list_using_loc = df.loc[:, 'Marks'].tolist()
print(marks_list_using_loc)
Output:
Name Marks
0 Martha 87
1 Tim 91
2 Rob 97
3 Georgia 95
[87, 91, 97, 95]
Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills and become a part of the hottest trend in the 21st century.
Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.
Last Updated :
30 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Add Your Comment
Please Login to comment...