Pandas DataFrame to List

Pandas DataFrame to List

Pandas is a powerful data manipulation library in Python that provides data structures and functions for effectively handling and analyzing large datasets. One of the most commonly used data structures in pandas is the DataFrame. A DataFrame is a two-dimensional, size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). In this article, we will explore various methods to convert a pandas DataFrame into a list.

1. Converting Entire DataFrame to List of Lists

One of the simplest ways to convert a DataFrame to a list is by using the values attribute followed by the tolist() method. This method converts the entire DataFrame into a list of lists, where each sublist represents a row in the DataFrame.

Example Code 1:

import pandas as pd

data = {'Website': ['pandasdataframe.com', 'example.com', 'test.com'],
        'Visits': [1000, 1500, 800]}
df = pd.DataFrame(data)
list_of_lists = df.values.tolist()
print(list_of_lists)

Output:

Pandas DataFrame to List

2. Converting DataFrame Columns to Lists

If you are interested in converting specific columns of a DataFrame into lists, you can access the column and then apply the tolist() method.

Example Code 2:

import pandas as pd

data = {'Website': ['pandasdataframe.com', 'example.com', 'test.com'],
        'Visits': [1000, 1500, 800]}
df = pd.DataFrame(data)
website_list = df['Website'].tolist()
print(website_list)

Output:

Pandas DataFrame to List

3. Converting DataFrame Rows to List of Dictionaries

Another useful method is converting each row of the DataFrame into a dictionary, with column names as keys. This can be achieved using the to_dict() method with the orient='records' parameter.

Example Code 3:

import pandas as pd

data = {'Website': ['pandasdataframe.com', 'example.com', 'test.com'],
        'Visits': [1000, 1500, 800]}
df = pd.DataFrame(data)
list_of_dicts = df.to_dict(orient='records')
print(list_of_dicts)

Output:

Pandas DataFrame to List

4. Converting Selective Columns to List of Tuples

You can also select multiple columns and convert them into a list of tuples. This is particularly useful when you need to preserve the relationship between different columns.

Example Code 4:

import pandas as pd

data = {'Website': ['pandasdataframe.com', 'example.com', 'test.com'],
        'Visits': [1000, 1500, 800],
        'Revenue': [200, 300, 150]}
df = pd.DataFrame(data)
columns_to_list = list(zip(df['Website'], df['Visits'], df['Revenue']))
print(columns_to_list)

Output:

Pandas DataFrame to List

5. Using apply() to Convert Rows to List

The apply() function can be used to apply a function along the axis of the DataFrame (rows or columns). This can be used to convert each row into a list.

Example Code 5:

import pandas as pd

data = {'Website': ['pandasdataframe.com', 'example.com', 'test.com'],
        'Visits': [1000, 1500, 800]}
df = pd.DataFrame(data)
rows_as_lists = df.apply(lambda row: row.tolist(), axis=1)
print(rows_as_lists)

Output:

Pandas DataFrame to List

6. Converting Index to List

If you need to convert the DataFrame index to a list, you can use the index.tolist() method.

Example Code 6:

import pandas as pd

data = {'Website': ['pandasdataframe.com', 'example.com', 'test.com'],
        'Visits': [1000, 1500, 800]}
df = pd.DataFrame(data)
index_list = df.index.tolist()
print(index_list)

Output:

Pandas DataFrame to List

7. Converting MultiIndex DataFrame to List

For DataFrames with MultiIndex, you can convert the indices to a list of tuples using the tolist() method on the MultiIndex.

Example Code 7:

import pandas as pd

tuples = [('pandasdataframe.com', 'A'), ('example.com', 'B'), ('test.com', 'C')]
index = pd.MultiIndex.from_tuples(tuples, names=['Website', 'Letter'])
data = {'Visits': [1000, 1500, 800]}
df = pd.DataFrame(data, index=index)
multiindex_list = df.index.tolist()
print(multiindex_list)

Output:

Pandas DataFrame to List

8. Converting DataFrame to List Using iterrows()

The iterrows() method returns an iterator yielding index and row data as pairs. This can be used to convert each row into a list or a tuple.

Example Code 8:

import pandas as pd

data = {'Website': ['pandasdataframe.com', 'example.com', 'test.com'],
        'Visits': [1000, 1500, 800]}
df = pd.DataFrame(data)
rows_as_tuples = [tuple(row) for index, row in df.iterrows()]
print(rows_as_tuples)

Output:

Pandas DataFrame to List

9. Converting DataFrame to List Using itertuples()

The itertuples() method returns an iterator yielding namedtuples of the values. This is a memory-efficient way to convert the DataFrame rows to tuples.

Example Code 9:

import pandas as pd

data = {'Website': ['pandasdataframe.com', 'example.com', 'test.com'],
        'Visits': [1000, 1500, 800]}
df = pd.DataFrame(data)
rows_as_namedtuples = list(df.itertuples(index=False, name='Row'))
print(rows_as_namedtuples)

Output:

Pandas DataFrame to List

10. Converting DataFrame to List by Column Names

If you want to convert a DataFrame to a list based on specific column names, you can select the columns first and then convert them to a list.

Example Code 10:

import pandas as pd

data = {'Website': ['pandasdataframe.com', 'example.com', 'test.com'],
        'Visits': [1000, 1500, 800],
        'Revenue': [200, 300, 150]}
df = pd.DataFrame(data)
selected_columns_list = df[['Website', 'Revenue']].values.tolist()
print(selected_columns_list)

Output:

Pandas DataFrame to List

In this article, we explored various methods to convert a pandas DataFrame to a list, covering different scenarios and needs. Whether you need the entire DataFrame as a list of lists, or specific columns as lists, or each row as a dictionary, pandas provides flexible and efficient solutions to achieve these conversions.