【问题标题】:Error: unsupported operand type(s) for /: 'str' and 'long' [duplicate]错误:/ 不支持的操作数类型:“str”和“long”[重复]
【发布时间】:2016-09-05 03:57:13
【问题描述】:

使用 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'**

【问题讨论】:

    标签: python-2.7 matplotlib seaborn kaggle


    【解决方案1】:

    基于@batmac 的回答。在命令中添加kind="count",解决了这个问题。

    示例:

    import pandas as pd 
    import seaborn as sns
    
    data = pd.read_csv('train.csv')
    sns.factorplot('Sex', data=data, kind="count")
    

    【讨论】:

      猜你喜欢
      • 2014-03-17
      • 2013-08-18
      • 2017-05-27
      • 2021-06-18
      • 2017-09-17
      • 2013-12-24
      • 2011-11-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多