Python 两个单元格之间的相关性
使用Python 两个单元格之间的相关性,python,python-3.x,dataframe,correlation,Python,Python 3.x,Dataframe,Correlation,使用df.corr(),我可以找到变量Likes和销售贡献之间的相关性。 我想找出每个品牌的喜好和销售贡献之间的相关性。 我该怎么做 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.Dat
df.corr()
,我可以找到变量Likes
和销售贡献
之间的相关性。
我想找出每个品牌的喜好和销售贡献之间的相关性。
我该怎么做
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 row in df:
print(df['Likes'][row].corr(df['Sales Contribution'][row]))
---------------------------------------------------------------------------
KeyError回溯(最近一次呼叫最后一次)
在里面
6 df=来自dict(数据)的pd.数据帧
7对于df中的行:
---->8打印(df['Likes'][row].corr(df['Sales Contribution'][row]))
E:\Anaconda\lib\site packages\pandas\core\series.py in\uuuuu getitem\uuuuuuu(self,key)
869 key=com.apply\u如果可调用(key,self)
870尝试:
-->871结果=self.index.get_值(self,key)
872
873如果不是标量(结果):
E:\Anaconda\lib\site packages\pandas\core\index\base.py in get\u value(self、series、key)
4402 k=self.\u convert\u scalar\u indexer(k,kind=“getitem”)
4403尝试:
->4404返回self.\u engine.get\u值(s,k,tz=getattr(series.dtype,“tz”,None))
4405除键错误为e1外:
4406如果len(self)>0且(self.holds_integer()或self.is_boolean()):
pandas\\u libs\index.pyx在pandas.\u libs.index.IndexEngine.get\u value()中
pandas\\u libs\index.pyx在pandas.\u libs.index.IndexEngine.get\u value()中
熊猫\\u libs\index.pyx在熊猫中。\ u libs.index.IndexEngine.get_loc()
pandas\\u libs\index\u class\u helper.pxi在pandas.\u libs.index.Int64Engine.\u check\u type()中
关键错误:“品牌”
在使用df.corr()之前,您需要将数据转换为数据帧,请尝试以下操作
---------------------------------------------------------------------------
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'
作为pd进口熊猫
你的dic
df=pd.DataFrame.from_dict(数据)
对于df中的行:我已经更新了代码,但仍然包含错误。请参考更新后的问题。df.iterrows()中的索引行使用:在上面的代码中,它解决了keyerror,但corr函数将nan作为输出,请查看文档,我不确定您在相关部分尝试执行什么操作。我已经更新了代码,但仍然包含错误。请参阅更新的问题。
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]))