Table of Contents
Use Series.values.tolist() Method
To convert pandas dataframe column to list:
- Use
pd.DataFrame()to readposition_salariesas apandasdata frame. - Use
df["Position"]to get the columnpositionfromdf - Use
position.valuesto get values of theposition - Use
position_values.tolist()to get list ofposition_valuesasposition_list
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import pandas as pd<br> # Create a pandas data frame position_salaries = { 'Position': ["Business Analyst", "Junior Consultant", "Senior Consultant", "Manager", "Country Manager", "Region Manager", "Partner", "Senior Partner", "C-level", "CEO"], 'Level': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'Salary': [45000, 50000, 60000, 80000, 110000, 150000, 200000, 300000, 500000, 1000000] } df = pd.DataFrame(position_salaries) position = df["Position"] position_values = position.values position_list = position_values.tolist() print(column_list) |
The code above will print the following output on the console:
|
1 2 3 |
['Business Analyst', 'Junior Consultant', 'Senior Consultant', 'Manager', 'Country Manager', 'Region Manager', 'Partner', 'Senior Partner', 'C-level', 'CEO'] |
Suppose we already have a Python library called pandas installed for working with data frames. If we don’t have it, we can install it using pip install pandas.
We imported the Python library pandas as pd.
pd.DataFrame() is a class of Python two-dimensional data structures used for data analysis and manipulation. We used it to read positon_salaries and to store the data frame in df.
In the pandas data frame, we can get columns of the data frame using the data retrieval technique that we use when working with arrays. The df["Position"] took the column name and stored the column in position.
In the Series.values function:
Series()is an n-dimensional array that holds data of any type. You may have questions about where thisSeriesis in the code. Here thepositionis aSeriescreated usingdf["Position"].Series.valuesreads theSeriesand returns an object of typenumpy.ndarray.
Once we got a series called position, we used position.values to get position_values.
tolist() is a predefined method in Python that does not hold any parameter. When we apply it to any iterable object, it converts that object to a list. We used it on our numpy.ndarray type position_values to get position_list.
Further reading:
Use list() Method
To convert the pandas data frame column to list:
- Use
pd.DataFrame()to readposition_salariesas apandasdata frame. - Use
df["Position"]to get the columnpositionfromdf - Use
position.valuesto get values of theposition - Use
list()to convertposition_valuestoposition_list
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import pandas as pd<br> position_salaries = { 'Position': ["Business Analyst", "Junior Consultant", "Senior Consultant", "Manager", "Country Manager", "Region Manager", "Partner", "Senior Partner", "C-level", "CEO"], 'Level': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'Salary': [45000, 50000, 60000, 80000, 110000, 150000, 200000, 300000, 500000, 1000000] } df = pd.DataFrame(position_salaries) position = df["Position"] position_values = position.values position_list = list(position_values) print(position_list) |
The code above will print the following output on the console:
|
1 2 3 |
['Business Analyst', 'Junior Consultant', 'Senior Consultant', 'Manager', 'Country Manager', 'Region Manager', 'Partner', 'Senior Partner', 'C-level', 'CEO'] |
We covered pd.DataFrame(), position, position_values, and Series while explaining the code using the Series.values.tolist() method.
In this code snippet, we used the list() method that takes an iterable object as an argument and creates an immutable list of that object. We passed position_values as an iterable Series to convert it to position_list.
Conclusion
You can use tolist() or list() methods to convert the pandas dataframe column to list.
You should always prefer tolist() over list() as tolist() is much faster than list() method.
That’s all about how to convert pandas dataframe column to list.