{"id":4471,"date":"2021-04-29T18:18:52","date_gmt":"2021-04-29T12:48:52","guid":{"rendered":"https:\/\/python-programs.com\/?p=4471"},"modified":"2021-11-22T18:53:42","modified_gmt":"2021-11-22T13:23:42","slug":"pandas-convert-a-dataframe-column-into-a-list-using-series-to_list-or-numpy-ndarray-tolist-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/pandas-convert-a-dataframe-column-into-a-list-using-series-to_list-or-numpy-ndarray-tolist-in-python\/","title":{"rendered":"Pandas: Convert a dataframe column into a list using Series.to_list() or numpy.ndarray.tolist() in python"},"content":{"rendered":"
This article is all about how to get a list of a specified column of a Pandas DataFrame using different methods.<\/p>\n
Lets create a dataframe which we will use in this article.<\/p>\n
import pandas as pd \r\nstudents = [('juli', 34, 'Sydney', 155),\r\n ('Ravi', 31, 'Delhi', 177.5),\r\n ('Aaman', 16, 'Mumbai', 81),\r\n ('Mohit', 31, 'Delhi', 167),\r\n ('Veena', 12, 'Delhi', 144),\r\n ('Shan', 35, 'Mumbai', 135),\r\n ('Sradha', 35, 'Colombo', 111)\r\n ]\r\n\r\nstudent_df = pd.DataFrame(students, columns=['Name', 'Age', 'City', 'Score'])\r\nprint(student_df)\r\n<\/pre>\nOutput:<\/strong><\/p>\n
Name\u00a0 \u00a0 Age\u00a0 \u00a0City\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 Score\r\n0\u00a0 \u00a0 \u00a0 Julie\u00a0 \u00a0 \u00a0 34\u00a0 \u00a0 \u00a0Sydney\u00a0 \u00a0 155.0\r\n1\u00a0 \u00a0 \u00a0Ravi\u00a0 \u00a0 \u00a0 \u00a031\u00a0 \u00a0 \u00a0 Delhi\u00a0 \u00a0 \u00a0 177.5\r\n2\u00a0 \u00a0 \u00a0Aman\u00a0 \u00a0 16\u00a0 \u00a0Mumbai\u00a0 \u00a0 \u00a0 81.0\r\n3\u00a0 \u00a0 \u00a0Mohit\u00a0 \u00a0 31\u00a0 \u00a0 \u00a0Delhi\u00a0 \u00a0 \u00a0 \u00a0 167.0\r\n4\u00a0 \u00a0 \u00a0Veena\u00a0 \u00a0 12\u00a0 \u00a0 \u00a0Delhi\u00a0 \u00a0 \u00a0 \u00a0 144.0\r\n5\u00a0 \u00a0 \u00a0 Shan\u00a0 \u00a0 \u00a035\u00a0 \u00a0 Mumbai\u00a0 \u00a0 135.0\r\n6\u00a0 \u00a0 \u00a0Sradha\u00a0 \u00a035\u00a0 Colombo\u00a0 \u00a0 111.0\r\n\r\n<\/pre>\nNow we are going to fetch a single column .<\/p>\n
There are different ways to do that.<\/p>\n
using Series.to_list()<\/h3>\n
We will use the same example we use above in this article.We select the column ‘Name’ .We will use [] that gives a series object.
Series.to_list() <\/code>\u00a0this function we use provided by the Series class to convert the series object and return a list.<\/p>\n
import pandas as pd \r\nstudents = [('juli', 34, 'Sydney', 155),\r\n ('Ravi', 31, 'Delhi', 177.5),\r\n ('Aaman', 16, 'Mumbai', 81),\r\n ('Mohit', 31, 'Delhi', 167),\r\n ('Veena', 12, 'Delhi', 144),\r\n ('Shan', 35, 'Mumbai', 135),\r\n ('Sradha', 35, 'Colombo', 111)\r\n ]\r\n\r\nstudent_df = pd.DataFrame(students, columns=['Name', 'Age', 'City', 'Score'])\r\nlist_of_names = student_df['Name'].to_list()\r\nprint('List of Names: ', list_of_names)\r\nprint('Type of listOfNames: ', type(list_of_names))\r\n<\/pre>\nOutput:<\/strong><\/p>\n
RESTART: C:\/Users\/HP\/Desktop\/article2.py\r\nList of Names: ['juli', 'Ravi', 'Aaman', 'Mohit', 'Veena', 'Shan', 'Sradha']\r\nType of listOfNames: <class 'list'>\r\n<\/pre>\nSo in above example you have seen its working…let me explain in brief..<\/p>\n
We have first select the column \u2018Name<\/em>\u2019 from the dataframe using [] operator,it returns a series object names, and we have confirmed that by printing its type.<\/p>\n
We used [] operator that gives a series object.
Series.to_list() <\/code>\u00a0this function we use provided by the series class to convert the series object and return a list.<\/p>\n
This is how we converted a dataframe column into a list.<\/p>\n
using numpy.ndarray.tolist()<\/h3>\n
From the give dataframe we will select the column \u201cName\u201d using a [] operator that returns a Series object and uses<\/p>\n
Series.Values to get a NumPy array from the series object. Next, we will use the function tolist() provided by NumPy array to convert it to a list.<\/p>\n
import pandas as pd \r\nstudents = [('juli', 34, 'Sydney', 155),\r\n ('Ravi', 31, 'Delhi', 177.5),\r\n ('Aaman', 16, 'Mumbai', 81),\r\n ('Mohit', 31, 'Delhi', 167),\r\n ('Veena', 12, 'Delhi', 144),\r\n ('Shan', 35, 'Mumbai', 135),\r\n ('Sradha', 35, 'Colombo', 111)\r\n ]\r\n\r\nstudent_df = pd.DataFrame(students, columns=['Name', 'Age', 'City', 'Score'])\r\nlist_of_names = student_df['Name'].values.tolist()\r\nprint('List of Names: ', list_of_names)\r\nprint('Type of listOfNames: ', type(list_of_names))\r\n\r\n<\/pre>\nOutput:<\/strong><\/p>\n
RESTART: C:\/Users\/HP\/Desktop\/article2.py\r\nList of Names: ['juli', 'Ravi', 'Aaman', 'Mohit', 'Veena', 'Shan', 'Sradha']\r\nType of listOfNames: <class 'list'>\r\n>>>\r\n\r\n<\/pre>\nSo now we are going to show you its working,<\/p>\n
We converted the column \u2018Name\u2019 into a list in a single line.Select the column \u2018Name\u2019 from the dataframe using [] operator,<\/p>\n
From Series.Values get a Numpy array<\/p>\n
import pandas as pd \r\nstudents = [('juli', 34, 'Sydney', 155),\r\n ('Ravi', 31, 'Delhi', 177.5),\r\n ('Aaman', 16, 'Mumbai', 81),\r\n ('Mohit', 31, 'Delhi', 167),\r\n ('Veena', 12, 'Delhi', 144),\r\n ('Shan', 35, 'Mumbai', 135),\r\n ('Sradha', 35, 'Colombo', 111)\r\n ]\r\n\r\nstudent_df = pd.DataFrame(students, columns=['Name', 'Age', 'City', 'Score'])\r\nnames = student_df['Name'].values\r\nprint('Numpy array: ', names)\r\nprint('Type of namesAsNumpy: ', type(names))<\/pre>\nOutput:<\/strong><\/p>\n
Numpy array: ['juli' 'Ravi' 'Aaman' 'Mohit' 'Veena' 'Shan' 'Sradha']\r\nType of namesAsNumpy: <class 'numpy.ndarray'>\r\n<\/pre>\nNumpy array provides a function tolist() to convert its contents to a list.<\/p>\n
This is how we selected our column \u2018Name\u2019 from Dataframe as a Numpy array and then turned it to a list.<\/p>\n
Conclusion:<\/strong><\/p>\n
In this article i have shown you that how to get a list of a specified column of a Pandas DataFrame using different methods.Enjoy learning guys.Thank you!<\/p>\n","protected":false},"excerpt":{"rendered":"
Get a list of a specified column of a Pandas DataFrame This article is all about how to get a list of a specified column of a Pandas DataFrame using different methods. Lets create a dataframe which we will use in this article. import pandas as pd students = [(‘juli’, 34, ‘Sydney’, 155), (‘Ravi’, 31, …<\/p>\n