'Intersecting dictionaries based on values

I have 2 dictionaries (dict1,dict2) consisting of three keys (gene_expression, Metadata, pValues) each containing large datasets. I would like to compare and contrast those two dictionaries to create a separate dictionary with common elements where pValue was <=0.005.

If I try:

intersection=dict(dict1.items() & dict2.items())

I get

unhashable type: 'DataFrame'

Ideally I would like to actually subset the items from both dictionaries where value is <=0.005

Thanks,



Solution 1:[1]

Demonstrate that the error 'unhashable type: 'DataFrame'' appears when attempting to use a dataframe as a key. This is because dataframes are mutable and keys cannot be mutable.

dict1 = {0: '0', 1: '1', 2: '2', 3: '3', 4: '4', 5: '5', 6: '6', 7: '7',
         8: '8', 9: '9'}
dict2 = {0: '0', 1: '1', 2: '2', 3: '3', 4: '4', 50: '50', 60: '60', 70: '70',
         80: '80', 90: '90'}

intersection = dict(dict1.items() & dict2.items())
print(f'{intersection=}')
intersection = dict1.items() & dict2.items()
print(f'{intersection=}')

import numpy as np
import pandas as pd

data = np.random.randint(5, 30, size=10)
df = pd.DataFrame(data, columns=['random_numbers'])

print(df)
dict3 = {df: 100}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1