Reputation: 1054
I'm trying to create a heatmap using Python on Pycharms. I've this code:
import numpy as np
import pandas as pd
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
data1 = pd.read_csv(FILE")
freqMap = {}
for line in data1:
for item in line:
if not item in freqMap:
freqMap[item] = {}
for other_item in line:
if not other_item in freqMap:
freqMap[other_item] = {}
freqMap[item][other_item] = freqMap[item].get(other_item, 0) + 1
freqMap[other_item][item] = freqMap[other_item].get(item, 0) + 1
df = data1[freqMap].T.fillna(0)
print(df)
My data is stored into a CSV file. Each row represents a sequence of products that are associated by a Consumer Transaction.The typically Basket Market Analysis:
99 32 35 45 56 58 7 72
99 45 51 56 58 62 72 17
55 56 58 62 21 99 35
21 99 44 56 58 7 72
72 17 99 35 45 56 7
56 62 72 21 91 99 35
99 35 55 56 58 62 72
99 35 51 55 58 7 21
99 56 58 62 72 21
55 56 58 21 99 35
99 35 62 7 17 21
62 72 21 99 35 58
56 62 72 99 32 35
72 17 99 55 56 58
When I execute the code, I'm getting the following error:
Traceback (most recent call last):
File "C:/Users/tst/PycharmProjects/untitled1/tes.py", line 22, in <module>
df = data1[freqMap].T.fillna(0)
File "C:\Users\tst\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\core\frame.py", line 1997, in __getitem__
return self._getitem_column(key)
File "C:\Users\tst\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\core\frame.py", line 2004, in _getitem_column
return self._get_item_cache(key)
File "C:\Users\tst\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\core\generic.py", line 1348, in _get_item_cache
res = cache.get(item)
TypeError: unhashable type: 'dict'
How can I solve this problem?
Many thanks!
Upvotes: 1
Views: 8686
Reputation: 2821
You are reading a csv file but it has no header, the delimiter is a space not a comma, and there are a variable number of columns. So that is three mistakes in your first line.
And data1 is a DataFrame, freqMap is a dictionary that is completely unrelated. So it makes no sense to do data1[freqMap].
I suggest you step through this line by line in jupyter or a python interpreter. Then you can see what each line actually does and experiment.
Upvotes: 2