pandas “DataFrame”对象不可调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40138380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'DataFrame' object is not callable
提问by Pedro Alves
I'm trying to create a heatmap using Python on Pycharms. I've this code:
我正在尝试在 Pycharms 上使用 Python 创建热图。我有这个代码:
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:
我的数据存储在一个 CSV 文件中。每行代表与消费者交易相关的一系列产品。典型的篮子市场分析:
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!
非常感谢!
回答by simon
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.
您正在读取一个 csv 文件,但它没有标题,分隔符是空格而不是逗号,并且列数是可变的。所以这是你的第一行中的三个错误。
And data1 is a DataFrame, freqMap is a dictionary that is completely unrelated. So it makes no sense to do data1[freqMap].
而data1是一个DataFrame,freqMap是一个完全不相关的字典。所以做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.
我建议您在 jupyter 或 python 解释器中逐行执行此操作。然后你可以看到每行实际做了什么并进行实验。