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 marks of all the students in a particular subject, get phone numbers of all employees, etc. Let’s see how we can achieve this with the help of some examples.
Example 1: We can have all values of a column in a list, by using the tolist() method.
Syntax: Series.tolist().
Return type: Converted series into List.
Code:
Python3
# import pandas libraeyimport pandas as pd # dictionarydict = {'Name': ['Martha', 'Tim', 'Rob', 'Georgia'], 'Marks': [87, 91, 97, 95]} # create a dataframe objectdf = pd.DataFrame(dict) # show the dataframeprint(df) # list of values of 'Marks' columnmarks_list = df['Marks'].tolist() # show the listprint(marks_list) |
Output:

Example 2: We’ll see how we can get the values of all columns in separate lists.
Code:
Python3
# import pandas libraryimport pandas as pd # dictionarydict = {'Name': ['Martha', 'Tim', 'Rob', 'Georgia'], 'Marks': [87, 91, 97, 95]} # create a dataframe objectdf = pd.DataFrame(dict) # show the dataframeprint(df) # iterating over and calling # tolist() method for # each columnfor i in list(df): # show the list of values print(df[i].tolist()) |
Output:



Please Login to comment...