How do I hash tuples in Python using pandas?

To hash tuples in Python using Pandas, you can convert the tuples into a format that is hashable (like a string) and then use the `hash()` function or any hashing library to generate a hash for each tuple. Below is an example demonstrating how to achieve this using the Pandas library.

import pandas as pd # Sample data: list of tuples data = [(1, 'apple'), (2, 'banana'), (3, 'cherry')] # Create a DataFrame df = pd.DataFrame(data, columns=['number', 'fruit']) # Define a function to hash tuples def hash_tuple(tuple): return hash(tuple) # Apply the hash function to each row in the DataFrame df['hash'] = df.apply(lambda row: hash_tuple((row['number'], row['fruit'])), axis=1) print(df)

hash tuples python pandas hashing data analysis