Python:ValueError:只能比较带有相同标记的Series对象

时间:2017-11-02 04:03:00

标签: python dataframe

检查了其他问题,但似乎没有一个问题。

一点新手,但是这里有:

执行时:

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:只能比较带有相同标签的系列对象

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

1 个答案:

答案 0 :(得分:0)

这可能是因为表达式没有以正确的顺序进行评估。试试这个:

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