首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >两个单元之间的相关性Python

两个单元之间的相关性Python
EN

Stack Overflow用户
提问于 2020-10-06 17:23:57
回答 2查看 41关注 0票数 0
代码语言:javascript
运行
复制
data = {'Brand':  ['Brand A', 'Brand B','Brand C','Brand D'],
        'Likes': [40500, 39400,25020,28900],
         'Sales Contribution': [0.019,0.307,0.21,0.13]
        }
df = pd.DataFrame.from_dict(data)

使用df.corr(),我可以找到变量LikesSales Contribution之间的相关性。我想找出每个品牌的喜欢和销售贡献之间的相关性。我该怎么做呢?

代码语言:javascript
运行
复制
for row in df:
    print(df['Likes'][row].corr(df['Sales Contribution'][row]))

结果:

代码语言:javascript
运行
复制
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-52-d54aac6b3ce8> in <module>
      6 df = pd.DataFrame.from_dict(data)
      7 for row in df:
----> 8     print(df['Likes'][row].corr(df['Sales Contribution'][row]))

E:\Anaconda\lib\site-packages\pandas\core\series.py in __getitem__(self, key)
    869         key = com.apply_if_callable(key, self)
    870         try:
--> 871             result = self.index.get_value(self, key)
    872 
    873             if not is_scalar(result):

E:\Anaconda\lib\site-packages\pandas\core\indexes\base.py in get_value(self, series, key)
   4402         k = self._convert_scalar_indexer(k, kind="getitem")
   4403         try:
-> 4404             return self._engine.get_value(s, k, tz=getattr(series.dtype, "tz", None))
   4405         except KeyError as e1:
   4406             if len(self) > 0 and (self.holds_integer() or self.is_boolean()):

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index_class_helper.pxi in pandas._libs.index.Int64Engine._check_type()

KeyError: 'Brand'
EN

回答 2

Stack Overflow用户

发布于 2020-10-06 17:37:40

在使用df.corr()之前,您需要将数据转换为dataframe,请尝试以下操作

代码语言:javascript
运行
复制
import pandas as pd
data = {'Brand':  ['Brand A', 'Brand B','Brand C','Brand D'],
    'Likes': [40500, 39400,25020,28900],
     'Sales Contribution': [0.019,0.307,0.21,0.13]
    }
df = pd.DataFrame.from_dict(data)
for index, row in df.iterrows():
    print(df['Likes'][row].corr(df['Sales Contribution'][row]))
票数 0
EN

Stack Overflow用户

发布于 2020-10-06 17:40:03

将pandas作为pd导入df中的行df= pd.DataFrame.from_dict(data):

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64222916

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档