【问题标题】:Python: ValueError: Can only compare identically-labeled Series objectsPython:ValueError:只能比较标记相同的系列对象
【发布时间】:2017-11-02 12:51:35
【问题描述】:

检查了关于此的其他问题,但似乎没有一个问题有我的具体问题。

有点新手,但这里是:

当我执行时:

dfm.mean()-dfm.std()*2

我明白了:

temperature    96.707104
dtype: float64

当我执行时:

dfm.loc[dfm['temperature'] < 96.707104]

我明白了:

    temperature gender
105 96.7    M
125 96.3    M

当我执行时:

dfm.loc[dfm['temperature'] < dfm.mean()-dfm.std()*2]

我明白了:

ValueErr       Traceback (most recent call last) <ipython-input-125-50749255f96c> in <module>()
----> 1 dfm.loc[dfm['temperature'] < dfm.mean()-dfm.std()*2]
C:\Applications\IDE-Compilers\WinPython-3.6.2\python-3.6.2.amd64\lib\site-packages\pandas\core\ops.py in wrapper(self, other, axis)
    816             if not self._indexed_same(other):
    817                 msg = 'Can only compare identically-labeled Series objects'
--> 818                 raise ValueError(msg)
    819             return self._constructor(na_op(self.values, other.values),
    820                                      index=self.index, name=name)


ValueError:只能比较标签相同的 Series 对象

Que pasa aqui?
即为什么我使用表达式时出现错误,而使用常量时却没有?

dfm的头尾:

    temperature gender
2   97.8    M
5   99.2    M
6   98.0    M
7   98.8    M
12  98.2    M

    temperature gender
118 98.7    M
124 97.5    M
125 96.3    M
126 97.7    M
128 97.9    M

?dfm

Type:        DataFrame
String form:
temperature gender
           2           97.8      M
           5           99.2      M
           6           98.0      M
           7 <...>  M
           125         96.3      M
           126         97.7      M
           128         97.9      M

           [65 rows x 2 columns]
Length:      65
File:        c:\applications\ide-compilers\winpython-3.6.2\python-3.6.2.amd64\lib\site-packages\pandas\core\frame.py
Docstring:  
Two-dimensional size-mutable, potentially heterogeneous tabular data
structure with labeled axes (rows and columns). Arithmetic operations
align on both row and column labels. Can be thought of as a dict-like
container for Series objects. The primary pandas data structure

【问题讨论】:

  • 从数据框 dfm 中提供几行。看起来您正在将熊猫系列 dfm['temperature'] 与标量值进行比较。
  • 完成。很想知道为什么我使用表达式时会出现错误,而使用常量时却没有。

标签: python dataframe


【解决方案1】:

这可能是因为您的表达式没有按正确的顺序进行计算。试试这个:

dfm[dfm['temperature'] < (dfm.mean()-dfm.std()*2)]

【讨论】:

    猜你喜欢
    • 2020-09-17
    • 2018-12-06
    • 1970-01-01
    • 2017-11-30
    • 1970-01-01
    • 1970-01-01
    • 2018-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多