You can get or convert the pandas DataFrame column to list using Series.values.tolist()
, since each column in DataFrame is represented as a Series internally, you can use this function after getting a column you wanted to convert as a Series. You can get a column as a Series by using df.column_name
or df['column_name']
.
1. Quick Exampls of Convert Column to List
Following are quick examples of how to convert column values to list in DataFrame.
# Quick Examples
# Using Series.values.tolist()
col_list = df.Courses.values.tolist()
print(col_list)
# Using Series.values.tolist()
col_list = df["Courses"].values.tolist()
print(col_list)
# Using list() Function
col_list = list(df["Courses"])
print(col_list)
# Conver to numpy array
col_list = df['Courses'].to_numpy()
print(col_list)
# Get by Column Index
col_list = df[df.columns[0]].values.tolist()
print(col_list)
# Convert Index Column to List
index_list = df.index.tolist()
print(index_list)
2. Convert pandas Column to list
By using Series.values.tolist()
you can convert pandas DataFrame Column to List. df[‘Courses’] returns the DataFrame column as a Series and then use values.tolist() to convert the column values to list.
# Using Series.values.tolist()
col_list = df.Courses.values.tolist()
print(col_list)
# Outputs:
['Spark', 'PySpark', 'Python', 'Pandas', 'Python', 'Spark', 'Pandas']
Below is an explanation of each section of the statement df.Courses.values.tolist()
.
df['Courses']
returns a Series object of a specified column.df['Courses'].values
returns an array with column values. and this has a helper function.tolist()
to convert to list.
# Get Column as Series
print(df.Courses)
# Output:
# 0 Spark
# 1 PySpark
# 2 Python
# 3 Pandas
# 4 Python
# 5 Spark
# 6 Pandas
# Name: Courses, dtype: object
# Get Column as Array
print(df.Courses.values)
# Output:
#['Spark' 'PySpark' 'Python' 'Pandas' 'Python' 'Spark' 'Pandas']
Alternatively, you can also write the statement using.
# Using Series.values.tolist()
col_list = df["Courses"].values.tolist()
To get unique values in column as a list use unique() function on the Series object.
3. Using list()
You can also pass the Series to python list() function to convert the column to list. The below example gets the values of a column Discount
as a list.
# Using list() Function
col_list = list(df["Discount"])
print(col_list)
# Output:
# [1000, 2300, 1200, 2000, 2300, 1000, 2000]
4. Conver Columns to Numpy Array
Sometimes you may be required to convert the pandas column to Numpy Array you can do so by using to_numpy() function of DataFrame.
# Conver to numpy.to_numpy() array
col_list = df['Courses'].to_numpy()
print(col_list)
print(type(col_list))
# Output:
# ['Spark' 'PySpark' 'Python' 'Pandas' 'Python'
# 'Spark' 'Pandas']
# <class 'numpy.ndarray'>
5. Get List by Column Index
If you have a column index and wanted to get the column values of an index, first you have to get the column name by index and then use the approaches explained above to convert it to a list. From the below example df.columns[0]
returns the column name for an index 0, which is Courses
.
# Get by Column Index
col_list = df[df.columns[0]].values.tolist()
print(col_list)
6. Convert Index Column to List
In case you wanted to get the index column as a list use DataFrame.index.tolist()
method. Even here DataFrame.index
returns the Index object and tolist()
converts it to list.
# Convert Index Column to List
index_list = df.index.tolist()
print(index_list)
# Output:
# [0, 1, 2, 3, 4, 5, 6]
7. Complete Example of pandas Conver Column to List
import pandas as pd
import numpy as np
technologies = {
'Courses':["Spark","PySpark","Python","Pandas","Python","Spark","Pandas"],
'Fee' :[20000,25000,22000,30000,25000,20000,30000],
'Duration':['30days','40days','35days','50days','40days','30days','50days'],
'Discount':[1000,2300,1200,2000,2300,1000,2000]
}
df = pd.DataFrame(technologies)
print(df)
# Using Series.values.tolist()
col_list = df.Courses.values.tolist()
print(col_list)
# Using Series.values.tolist()
col_list = df["Courses"].values.tolist()
print(col_list)
# Using list() Function
col_list = list(df["Courses"])
print(col_list)
# Get by Column Index
col_list = df[df.columns[0]].values.tolist()
print(col_list)
# Convert Index Column to List
index_list = df.index.tolist()
print(index_list)
Conclusion
In this article, you have learned how to get the column values as a list by using several methods like Series.values, list() and also learned converting index column to list with examples.
Happy Learning !!
Related Articles
- How to Combine Two Columns of Text in Pandas DataFrame
- What is a Pandas DataFrame Explained With Examples
- Series.tolist() – Convert Pandas Series to List
- Convert Pandas Series of Lists to One Series
- How to Convert List to Pandas Series
- Pandas Create DataFrame From List
- Pandas Convert List of Dictionaries to DataFrame
- Pandas Insert List into Cell of DataFrame