How to print a list in columns Python

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


# Using Series.values.tolist()
col_list = df.Courses.values.tolist()
print(col_list)

# Outputs
['Spark', 'PySpark', 'Python', 'Pandas', 'Python', 'Spark', 'Pandas']
0 or

# Using Series.values.tolist()
col_list = df.Courses.values.tolist()
print(col_list)

# Outputs
['Spark', 'PySpark', 'Python', 'Pandas', 'Python', 'Spark', 'Pandas']
1.

PySpark Tutorial For Beginners | Py...

Please enable JavaScript

PySpark Tutorial For Beginners | Python Examples

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


# Using Series.values.tolist()
col_list = df.Courses.values.tolist()
print(col_list)

# Outputs
['Spark', 'PySpark', 'Python', 'Pandas', 'Python', 'Spark', 'Pandas']
3.

  • 
    # Using Series.values.tolist()
    col_list = df.Courses.values.tolist()
    print(col_list)
    
    # Outputs
    ['Spark', 'PySpark', 'Python', 'Pandas', 'Python', 'Spark', 'Pandas']
    
    4 returns a Series object of a specified column.
  • 
    # Using Series.values.tolist()
    col_list = df.Courses.values.tolist()
    print(col_list)
    
    # Outputs
    ['Spark', 'PySpark', 'Python', 'Pandas', 'Python', 'Spark', 'Pandas']
    
    5 returns an array with column values. and this has a helper function 
    
    # Using Series.values.tolist()
    col_list = df.Courses.values.tolist()
    print(col_list)
    
    # Outputs
    ['Spark', 'PySpark', 'Python', 'Pandas', 'Python', 'Spark', 'Pandas']
    
    6 to convert to list.

# Get Column as Series
print(df.Courses)
#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)
#['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


# Using Series.values.tolist()
col_list = df.Courses.values.tolist()
print(col_list)

# Outputs
['Spark', 'PySpark', 'Python', 'Pandas', 'Python', 'Spark', 'Pandas']
7 as a list.


# Using list() Function
col_list =  list(df["Discount"])

# Outputs
[1000, 2300, 1200, 2000, 2300, 1000, 2000]
print(col_list)

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))

# Outputs
['Spark' 'PySpark' 'Python' 'Pandas' 'Python' 'Spark' 'Pandas']

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


# Using Series.values.tolist()
col_list = df.Courses.values.tolist()
print(col_list)

# Outputs
['Spark', 'PySpark', 'Python', 'Pandas', 'Python', 'Spark', 'Pandas']
8 returns the column name for an index 0, which is

# Using Series.values.tolist()
col_list = df.Courses.values.tolist()
print(col_list)

# Outputs
['Spark', 'PySpark', 'Python', 'Pandas', 'Python', 'Spark', 'Pandas']
9.


# 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


# Get Column as Series
print(df.Courses)
#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)
#['Spark' 'PySpark' 'Python' 'Pandas' 'Python' 'Spark' 'Pandas']
0 method. Even here

# Get Column as Series
print(df.Courses)
#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)
#['Spark' 'PySpark' 'Python' 'Pandas' 'Python' 'Spark' 'Pandas']
1 returns the Index object and

# Get Column as Series
print(df.Courses)
#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)
#['Spark' 'PySpark' 'Python' 'Pandas' 'Python' 'Spark' 'Pandas']
2 converts it to list.


# Convert Index Column to List
index_list = df.index.tolist()
print(index_list)

# Outputs
[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.

How do I print a list vertically in Python?

Method #2 : Using zip() Using zip function, we map the elements at respective index to one other and after that print each of them. This performs the task of vertical printing.

How do I print a column list in Python?

3 Easy Ways to Print column Names in Python.
Using pandas. dataframe. columns to print column names in Python. ... .
Using pandas. dataframe. columns. ... .
Python sorted() method to get the column names. Python sorted() method can be used to get the list of column names of a dataframe in an ascending order of columns..

How do I print a list of strings as columns in Python?

To print a list in columns:.
Use the zip() function to get a zip object of tuples..
Use a formatted string literal to format the items in each tuple in a row..
Use the print() function to print the result..

How do I print a list side by side in Python?

You can use the zip() function to join lists together. The zip() function will iterate tuples with the corresponding elements from each of the lists, which you can then format as Michael Butscher suggested in the comments. Finally, just join() them together with newlines and you have the string you want.