错误:/:'str'和'long'的操作数类型不受支持

时间:2016-05-10 09:11:07

标签: python-2.7 matplotlib seaborn kaggle

使用python 2.7的Kaggle Titanic

import pandas as pd
from pandas import Series,DataFrame

titanic_df = pd.read_csv("train.csv")

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline

**sns.factorplot('Pclass',data=titanic_df,hue='Sex')**

获取错误

TypeError                                 Traceback (most recent call last)
<ipython-input-7-a5240a4b6a9f> in <module>()
      1 # Let's first check gender
----> 2 sns.factorplot('Sex',data=titanic_df)

C:\Users\bigminduser\Anaconda2\lib\site-packages\seaborn-0.7.0-py2.7.egg\seaborn\categorical.pyc in factorplot(x, y, hue, data, row, col, col_wrap, estimator, ci, n_boot, units, order, hue_order, row_order, col_order, kind, size, aspect, orient, color, palette, legend, legend_out, sharex, sharey, margin_titles, facet_kws, **kwargs)
   3365 
   3366     # Draw the plot onto the facets
-> 3367     g.map_dataframe(plot_func, x, y, hue, **plot_kws)
   3368 
   3369     # Special case axis labels for a count type plot

C:\Users\bigminduser\Anaconda2\lib\site-packages\seaborn-0.7.0-py2.7.egg\seaborn\axisgrid.pyc in map_dataframe(self, func, *args, **kwargs)
    791 
    792             # Draw the plot
--> 793             self._facet_plot(func, ax, args, kwargs)
    794 
    795         # Finalize the annotations and layout

C:\Users\bigminduser\Anaconda2\lib\site-packages\seaborn-0.7.0-py2.7.egg\seaborn\axisgrid.pyc in _facet_plot(self, func, ax, plot_args, plot_kwargs)
    809 
    810         # Draw the plot
--> 811         func(*plot_args, **plot_kwargs)
    812 
    813         # Sort out the supporting information

C:\Users\bigminduser\Anaconda2\lib\site-packages\seaborn-0.7.0-py2.7.egg\seaborn\categorical.pyc in pointplot(x, y, hue, data, order, hue_order, estimator, ci, n_boot, units, markers, linestyles, dodge, join, scale, orient, color, palette, ax, **kwargs)
   3009                             estimator, ci, n_boot, units,
   3010                             markers, linestyles, dodge, join, scale,
-> 3011                             orient, color, palette)
   3012 
   3013     if ax is None:

C:\Users\bigminduser\Anaconda2\lib\site-packages\seaborn-0.7.0-py2.7.egg\seaborn\categorical.pyc in __init__(self, x, y, hue, data, order, hue_order, estimator, ci, n_boot, units, markers, linestyles, dodge, join, scale, orient, color, palette)
   1575                                  order, hue_order, units)
   1576         self.establish_colors(color, palette, 1)
-> 1577         self.estimate_statistic(estimator, ci, n_boot)
   1578 
   1579         # Override the default palette for single-color plots

C:\Users\bigminduser\Anaconda2\lib\site-packages\seaborn-0.7.0-py2.7.egg\seaborn\categorical.pyc in estimate_statistic(self, estimator, ci, n_boot)
   1434                     statistic.append(np.nan)
   1435                 else:
-> 1436                     statistic.append(estimator(stat_data))
   1437 
   1438                 # Get a confidence interval for this estimate

C:\Users\bigminduser\Anaconda2\lib\site-packages\numpy\core\fromnumeric.pyc in mean(a, axis, dtype, out, keepdims)
   2872 
   2873     return _methods._mean(a, axis=axis, dtype=dtype,
-> 2874                           out=out, keepdims=keepdims)
   2875 
   2876 

C:\Users\bigminduser\Anaconda2\lib\site-packages\numpy\core\_methods.pyc in _mean(a, axis, dtype, out, keepdims)
     70         ret = ret.dtype.type(ret / rcount)
     71     else:
---> 72         ret = ret / rcount
     73 
     74     return ret

**TypeError: unsupported operand type(s) for /: 'str' and 'long'**

2 个答案:

答案 0 :(得分:5)

基于@batmac的答案。在命令中添加import pandas as pd import seaborn as sns data = pd.read_csv('train.csv') sns.factorplot('Sex', data=data, kind="count") ,解决了问题。

示例:

{{1}}

答案 1 :(得分:3)

嘿,我也有同样的问题!我在这里找到了答案:

Issue with seaborn in sublime

希望它对你有用!