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 libraey import pandas as pd # dictionary dict = { 'Name' : [ 'Martha' , 'Tim' , 'Rob' , 'Georgia' ], 'Marks' : [ 87 , 91 , 97 , 95 ]} # create a dataframe object df = pd.DataFrame( dict ) # show the dataframe print (df) # list of values of 'Marks' column marks_list = df[ 'Marks' ].tolist() # show the list 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 library import pandas as pd # dictionary dict = { 'Name' : [ 'Martha' , 'Tim' , 'Rob' , 'Georgia' ], 'Marks' : [ 87 , 91 , 97 , 95 ]} # create a dataframe object df = pd.DataFrame( dict ) # show the dataframe print (df) # iterating over and calling tolist() # method for each column for i in list (df): # show the list of values 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 libraey import pandas as pd # dictionary dict = { 'Name' : [ 'Martha' , 'Tim' , 'Rob' , 'Georgia' ], 'Marks' : [ 87 , 91 , 97 , 95 ]} # create a dataframe object df = pd.DataFrame( dict ) # show the dataframe print (df) # Using get() to get a list of values from the 'Marks' column marks_column = df.get( 'Marks' ) # Convert the Pandas Series to a Python list marks_list_using_get = marks_column.tolist() # Show the list 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 library import pandas as pd # dictionary dict = { 'Name' : [ 'Martha' , 'Tim' , 'Rob' , 'Georgia' ], 'Marks' : [ 87 , 91 , 97 , 95 ]} # create a dataframe object df = pd.DataFrame( dict ) # show the dataframe print (df) # Using .loc[] to get a list of values from the 'Marks' column marks_list_using_loc = df.loc[:, 'Marks' ].tolist() # Show the list print (marks_list_using_loc) |
Output:
Name Marks
0 Martha 87
1 Tim 91
2 Rob 97
3 Georgia 95
[87, 91, 97, 95]
Get a list of a particular column values of a Pandas DataFrame
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.