【问题标题】:I'm getting an error when I try to run this code - unsupported operand type(s) for /: 'str' and 'int'尝试运行此代码时出现错误 - /: 'str' 和 'int' 的操作数类型不受支持
【发布时间】:2018-11-30 20:29:05
【问题描述】:
import matplotlib as plt
%matplotlib inline
import numpy as np
import pandas as pd
from pandas import Series, DataFrame
import seaborn as sns

df.head()
Out[46]: 
   PassengerId  Survived  Pclass    ...        Fare Cabin  Embarked
0            1         0       3    ...      7.2500   NaN         S
1            2         1       1    ...     71.2833   C85         C
2            3         1       3    ...      7.9250   NaN         S
3            4         1       1    ...     53.1000  C123         S
4            5         0       3    ...      8.0500   NaN         S

[5 rows x 12 columns]

df = pd.read_csv('D:\dframe.csv')

sns.factorplot('Sex', data = df)

错误:

Traceback (most recent call last):

  File "<ipython-input-45-703be846d61b>", line 1, in <module>
    sns.factorplot('Sex', data = df)

  File "C:\ProgramData\Anaconda3\lib\site-packages\seaborn\categorical.py", line 3530, in factorplot
    g.map_dataframe(plot_func, x, y, hue, **plot_kws)

  File "C:\ProgramData\Anaconda3\lib\site-packages\seaborn\axisgrid.py", line 809, in map_dataframe
    self._facet_plot(func, ax, args, kwargs)

  File "C:\ProgramData\Anaconda3\lib\site-packages\seaborn\axisgrid.py", line 827, in _facet_plot
    func(*plot_args, **plot_kwargs)

  File "C:\ProgramData\Anaconda3\lib\site-packages\seaborn\categorical.py", line 3143, in pointplot
    orient, color, palette, errwidth, capsize)

  File "C:\ProgramData\Anaconda3\lib\site-packages\seaborn\categorical.py", line 1669, in __init__
    self.estimate_statistic(estimator, ci, n_boot)

  File "C:\ProgramData\Anaconda3\lib\site-packages\seaborn\categorical.py", line 1482, in estimate_statistic
    statistic.append(estimator(stat_data))

  File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py", line 2957, in mean
    out=out, **kwargs)

  File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\core\_methods.py", line 82, in _mean
    ret = ret / rcount

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

我使用的数据集是一个 csv 文件“train.csv”:

【问题讨论】:

  • 你能给我们看看 df.head() 的输出吗
  • @tobsecret df.head() Out[46]:PassengerId Survived Pclass ... 票价客舱登机 0 1 0 3 ... 7.2500 NaN S 1 2 1 1 ... 71.2833 C85 C 2 3 1 3 ... 7.9250 NaN S 3 4 1 1 ... 53.1000 C123 S 4 5 0 3 ... 8.0500 NaN S [5 行 x 12 列]
  • @DevenBothra 你能把它格式化放在问题中吗。不在 cmets 中。

标签: python python-3.x pandas numpy


【解决方案1】:

您正在使用因子图,但您只提供了一个 x 值 (Sex),因此它无法确定要为您绘制的内容。因子图旨在用于根据某些分类变量创建FacetGrid 的图。 您正在寻找的可能是sns.countplot,请参阅示例here

sns.countplot('Sex', data = df)

或者,如果您只是想检查不同数值变量之间的所有关系,您可以使用sns.pairplot

sns.pairplot(df)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-28
    • 2014-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-03
    • 1970-01-01
    • 2013-10-12
    相关资源
    最近更新 更多