1
我正嘗試在Pycharms上使用Python創建一個heatmap。我有這樣的代碼:'DataFrame'對象不可調用
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)
我的數據存儲在一個CSV文件中。每一行代表的是由消費者Transaction.The通常籃市場分析相關產品的序列:
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
當我執行的代碼,我發現了以下錯誤:
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'
如何我可以解決這個問題嗎?
非常感謝!
@ZachS。非常感謝您的迴應。我試着用你的說法,但我仍然有同樣的錯誤:( –
你試過了:df = data1 [freqMap] .T.fillna(0) –
你試圖得到什麼輸出? –