Sling Academy
Home/Pandas/Pandas: Converting a DataFrame to a list of tuples (3 examples)

Pandas: Converting a DataFrame to a list of tuples (3 examples)

Last updated: February 19, 2024

Introduction

Pandas is a versatile tool in the Python data science toolkit that allows for efficient manipulation and analysis of data. A common operation while working with Pandas DataFrames is converting them into a list of tuples. This conversion can greatly simplify passing DataFrame data into functions that require tuples, data serialization, or even for database operations. In this tutorial, we will explore various methods to convert a Pandas DataFrame to a list of tuples, with code examples that increase in complexity from basic to advanced.

Basic Conversion

The simplest way to convert a DataFrame into a list of tuples is to use the .itertuples() method. This method iterates over the DataFrame rows as named tuples. Here’s a basic example:

import pandas as pd

df = pd.DataFrame({
    'Name': ['Alice', 'Bob', 'Charlie'],
    'Age': [25, 30, 35],
    'City': ['New York', 'Paris', 'London']
})

list_of_tuples = list(df.itertuples(index=False, name=None))
print(list_of_tuples)

The output will be:

[("Alice", 25, "New York"), ("Bob", 30, "Paris"), ("Charlie", 35, "London")]

Using .to_numpy() for Conversion

An alternative method to convert a DataFrame to a list of tuples involves using the .to_numpy() function and then converting the numpy array to a list of tuples. Here’s how you can do it:

df_numpy = df.to_numpy()
list_of_tuples_via_numpy = [tuple(row) for row in df_numpy]
print(list_of_tuples_via_numpy)

The output remains similar:

[("Alice", 25, "New York"), ("Bob", 30, "Paris"), ("Charlie", 35, "London")]

Customized Tuple Conversion

Sometimes, you may need to customize how the tuples are created, especially when dealing with large DataFrames or when specific data manipulation is required before the conversion. This can be achieved by applying functions to DataFrame rows. Here’s an advanced example:

def custom_tuple(row):
    return (row['Name'].upper(), row['Age'], row['City'].capitalize())

list_of_custom_tuples = [custom_tuple(row) for _, row in df.iterrows()]
print(list_of_custom_tuples)

The output will demonstrate the custom transformation:

[("ALICE", 25, "New york"), ("BOB", 30, "Paris"), ("CHARLIE", 35, "London")]

Conclusion

Converting Pandas DataFrames to a list of tuples is a fundamental operation that can be achieved through various methods, depending on your specific requirements. We explored three main approaches: leveraging .itertuples() for straightforward conversion, utilizing .to_numpy() for a more numpy-centric method, and customizing tuple creation with functions for more complex scenarios. Knowing how to seamlessly transition between these forms can greatly enhance your data manipulation capabilities in Python.

Next Article: A deep dive into pandas.DataFrame.xs() method

Previous Article: Pandas: How to create a DataFrame from a list of tuples (5 examples)

Series: DateFrames in Pandas

Pandas

You May Also Like

  • How to Use Pandas Profiling for Data Analysis (4 examples)
  • How to Handle Large Datasets with Pandas and Dask (4 examples)
  • Pandas – Using DataFrame.pivot() method (3 examples)
  • Pandas: How to ‘FULL JOIN’ 2 DataFrames (3 examples)
  • Pandas: Select columns whose names start/end with a specific string (4 examples)
  • 3 ways to turn off future warnings in Pandas
  • How to Integrate Pandas with Apache Spark
  • How to Use Pandas for Web Scraping and Saving Data (2 examples)
  • How to Clean and Preprocess Text Data with Pandas (3 examples)
  • Pandas – Using Series.replace() method (3 examples)
  • Pandas json_normalize() function: Explained with examples
  • Pandas: Reading CSV and Excel files from AWS S3 (4 examples)
  • Using pandas.Series.rank() method (4 examples)
  • Pandas: Dropping columns whose names contain a specific string (4 examples)
  • Pandas: How to print a DataFrame without index (3 ways)
  • Fixing Pandas NameError: name ‘df’ is not defined
  • Pandas – Using DataFrame idxmax() and idxmin() methods (4 examples)
  • Pandas FutureWarning: ‘M’ is deprecated and will be removed in a future version, please use ‘ME’ instead
  • Pandas: Checking equality of 2 DataFrames (element-wise)