In [32]:
import pandas as pd
import plotly
import plotly.plotly as py
from plotly.graph_objs import *
import matplotlib.pyplot as plt
import plotly.tools as tls

In [ ]:
plotly.tools.set_credentials_file(username='DemoAccount', api_key='lr1c37zw81')

In [87]:
df = pd.read_csv('../data/cs-training.csv')
del df['Unnamed: 0']

In [22]:
for column in df.columns.values:
    if "" in column:
        df[column].fillna(df[column].mean(), inplace=True)

In [26]:
df = df.dropna()

In [89]:
for column in df.columns.values:
    data_bar_charts = {}
    x = df.groupby('SeriousDlqin2yrs', as_index=False)[column].mean()
    data_bar_charts = dict(zip(x[0], x[1]))
    print(x)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-89-3ced06d5043d> in <module>()
      2     data_bar_charts = {}
      3     x = df.groupby('SeriousDlqin2yrs', as_index=False)[column].mean()
----> 4     data_bar_charts = dict(zip(x[0], x[1]))
      5     print(x)
      6 

TypeError: zip argument #1 must support iteration

In [69]:
te = {}

In [ ]:


In [99]:
te = df.groupby('SeriousDlqin2yrs', as_index=False)['MonthlyIncome'].mean()

In [112]:
zip(te.iloc[0]).__next__()


Out[112]:
(0.0,)

In [77]:
dict(zip(x['NumberOfDependents'], x['SeriousDlqin2yrs']))

x.columns[0]


---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
C:\ProgramData\Miniconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2441             try:
-> 2442                 return self._engine.get_loc(key)
   2443             except KeyError:

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5280)()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5126)()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas\_libs\hashtable.c:20523)()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas\_libs\hashtable.c:20477)()

KeyError: 'NumberOfDependents'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-77-10968e4d3845> in <module>()
----> 1 dict(zip(x['NumberOfDependents'], x['SeriousDlqin2yrs']))
      2 
      3 x.columns[0]

C:\ProgramData\Miniconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   1962             return self._getitem_multilevel(key)
   1963         else:
-> 1964             return self._getitem_column(key)
   1965 
   1966     def _getitem_column(self, key):

C:\ProgramData\Miniconda3\lib\site-packages\pandas\core\frame.py in _getitem_column(self, key)
   1969         # get column
   1970         if self.columns.is_unique:
-> 1971             return self._get_item_cache(key)
   1972 
   1973         # duplicate columns & possible reduce dimensionality

C:\ProgramData\Miniconda3\lib\site-packages\pandas\core\generic.py in _get_item_cache(self, item)
   1643         res = cache.get(item)
   1644         if res is None:
-> 1645             values = self._data.get(item)
   1646             res = self._box_item_values(item, values)
   1647             cache[item] = res

C:\ProgramData\Miniconda3\lib\site-packages\pandas\core\internals.py in get(self, item, fastpath)
   3588 
   3589             if not isnull(item):
-> 3590                 loc = self.items.get_loc(item)
   3591             else:
   3592                 indexer = np.arange(len(self.items))[isnull(self.items)]

C:\ProgramData\Miniconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2442                 return self._engine.get_loc(key)
   2443             except KeyError:
-> 2444                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2445 
   2446         indexer = self.get_indexer([key], method=method, tolerance=tolerance)

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5280)()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5126)()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas\_libs\hashtable.c:20523)()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas\_libs\hashtable.c:20477)()

KeyError: 'NumberOfDependents'

In [57]:
x = {'0': {
    'NumberOfDependents': df.NumberOfDependents.mean()
    
}}
x


Out[57]:
{'SeriousDlqin2yrs': {'NumberOfDependents': 0.75722226786056568}}

In [76]:
df.groupby('SeriousDlqin2yrs').apply(lambda x: dict(zip(x['SeriousDlqin2yrs'], x['NumberOfDependents'].mean())).to_dict())


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-76-551afab8b03e> in <module>()
----> 1 df.groupby('SeriousDlqin2yrs').apply(lambda x: dict(zip(x['SeriousDlqin2yrs'], x['NumberOfDependents'].mean())).to_dict())

C:\ProgramData\Miniconda3\lib\site-packages\pandas\core\groupby.py in apply(self, func, *args, **kwargs)
    714         # ignore SettingWithCopy here in case the user mutates
    715         with option_context('mode.chained_assignment', None):
--> 716             return self._python_apply_general(f)
    717 
    718     def _python_apply_general(self, f):

C:\ProgramData\Miniconda3\lib\site-packages\pandas\core\groupby.py in _python_apply_general(self, f)
    718     def _python_apply_general(self, f):
    719         keys, values, mutated = self.grouper.apply(f, self._selected_obj,
--> 720                                                    self.axis)
    721 
    722         return self._wrap_applied_output(

C:\ProgramData\Miniconda3\lib\site-packages\pandas\core\groupby.py in apply(self, f, data, axis)
   1800             # group might be modified
   1801             group_axes = _get_axes(group)
-> 1802             res = f(group)
   1803             if not _is_indexed_like(res, group_axes):
   1804                 mutated = True

<ipython-input-76-551afab8b03e> in <lambda>(x)
----> 1 df.groupby('SeriousDlqin2yrs').apply(lambda x: dict(zip(x['SeriousDlqin2yrs'], x['NumberOfDependents'].mean())).to_dict())

TypeError: zip argument #2 must support iteration

In [51]:
print (df.groupby(level='ISBN')
         .apply(lambda x: dict(zip(x['User-ID'], x['Book-Rating'])))
         .to_dict())


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-51-5e498d982c29> in <module>()
      1 print (df.groupby('SeriousDlqin2yrs')
----> 2          .apply(lambda x: dict(zip(x['SeriousDlqin2yrs'], x[NumberOfDependents].mean())))
      3          .to_dict())

C:\ProgramData\Miniconda3\lib\site-packages\pandas\core\groupby.py in apply(self, func, *args, **kwargs)
    714         # ignore SettingWithCopy here in case the user mutates
    715         with option_context('mode.chained_assignment', None):
--> 716             return self._python_apply_general(f)
    717 
    718     def _python_apply_general(self, f):

C:\ProgramData\Miniconda3\lib\site-packages\pandas\core\groupby.py in _python_apply_general(self, f)
    718     def _python_apply_general(self, f):
    719         keys, values, mutated = self.grouper.apply(f, self._selected_obj,
--> 720                                                    self.axis)
    721 
    722         return self._wrap_applied_output(

C:\ProgramData\Miniconda3\lib\site-packages\pandas\core\groupby.py in apply(self, f, data, axis)
   1800             # group might be modified
   1801             group_axes = _get_axes(group)
-> 1802             res = f(group)
   1803             if not _is_indexed_like(res, group_axes):
   1804                 mutated = True

<ipython-input-51-5e498d982c29> in <lambda>(x)
      1 print (df.groupby('SeriousDlqin2yrs')
----> 2          .apply(lambda x: dict(zip(x['SeriousDlqin2yrs'], x[NumberOfDependents].mean())))
      3          .to_dict())

NameError: name 'NumberOfDependents' is not defined

In [46]:
x


Out[46]:
SeriousDlqin2yrs
0    0.743787
1    0.944798
Name: NumberOfDependents, dtype: float64

In [40]:
df.groupby('SeriousDlqin2yrs', as_index=True)['age'].mean()


Out[40]:
SeriousDlqin2yrs
0    52.751375
1    45.926591
Name: age, dtype: float64

In [ ]: