'DataFrame'对象不可调用 [英] 'DataFrame' object is not callable

查看:196
本文介绍了'DataFrame'对象不可调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python在Pycharms上创建一个热图。我有这个代码:

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)

我的数据存储到CSV文件中,每行代表一系列产品通常是消费者交易。通常篮子市场分析:

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'

我解决了这个问题?

非常感谢!

推荐答案

你正在读一个csv文件,它没有标题,分隔符是一个不是逗号的空格,并且有一个可变数量的列。这是你第一行的三个错误。

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.

而data1是一个DataFrame,freqMap是完全不相关的字典。所以做data1 [freqMap]没有任何意义。

And data1 is a DataFrame, freqMap is a dictionary that is completely unrelated. So it makes no sense to do data1[freqMap].

我建议你在jupyter或者一个python解释器中一行一行。然后,您可以看到每一行实际执行和实验。

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.

这篇关于'DataFrame'对象不可调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆