0
votes
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)

Using df.corr(), I can find the correlation between the variables Likes and Sales Contribution. I would like to find the correlation between likes and sales contribution for each brand. How can I do that?

for row in df:
    print(df['Likes'][row].corr(df['Sales Contribution'][row]))

results in

---------------------------------------------------------------------------
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'

2

2 Answers

0
votes

You need to convert your data to dataframe before using df.corr(), try this

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]))
-1
votes

import pandas as pd your dic df = pd.DataFrame.from_dict(data) for row in df: