pandas:当数据是NaN时,逻辑操作无法完成

时间:2016-02-08 20:01:27

标签: python pandas

我在Pandas中有一个大型DataFrame,当没有分配给任何值时,2列可以有值或NaN(Null)。

我想基于这些填充第3列2.当不是NaN时,它需要一些值。其工作原理如下:

In [16]: import pandas as pd

In [17]: import numpy as np

In [18]: df = pd.DataFrame([[np.NaN, np.NaN],['John', 'Malone'],[np.NaN, np.NaN]], columns = ['col1', 'col2'])

In [19]: df
Out[19]:
   col1    col2
0   NaN     NaN
1  John  Malone
2   NaN     NaN

In [20]: df['col3'] = np.NaN

In [21]: df.loc[df['col1'].notnull(),'col3'] = 'I am ' + df['col1']

In [22]: df
Out[22]:
   col1    col2       col3
0   NaN     NaN        NaN
1  John  Malone  I am John
2   NaN     NaN        NaN

这也有效:

In [29]: df.loc[df['col1']== 'John','col3'] = 'I am ' + df['col2']

In [30]: df
Out[30]:
   col1    col2         col3
0   NaN     NaN          NaN
1  John  Malone  I am Malone
2   NaN     NaN          NaN

但是,如果我没有使所有值NaN然后尝试最后一个loc,它会给我一个错误!

In [31]: df = pd.DataFrame([[np.NaN, np.NaN],[np.NaN, np.NaN],[np.NaN, np.NaN]], columns = ['col1', 'col2'])

In [32]: df
Out[32]:
   col1  col2
0   NaN   NaN
1   NaN   NaN
2   NaN   NaN

In [33]: df['col3'] = np.NaN

In [34]: df.loc[df['col1']== 'John','col3'] = 'I am ' + df['col2']
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
c:\python33\lib\site-packages\pandas\core\ops.py in na_op(x, y)
    552             result = expressions.evaluate(op, str_rep, x, y,
--> 553                                           raise_on_error=True, **eval_kwargs)
    554         except TypeError:

c:\python33\lib\site-packages\pandas\computation\expressions.py in evaluate(op, op_str, a, b, raise_on_error, use_numexpr, **eval_kwargs)
    217         return _evaluate(op, op_str, a, b, raise_on_error=raise_on_error,
--> 218                          **eval_kwargs)
    219     return _evaluate_standard(op, op_str, a, b, raise_on_error=raise_on_error)

c:\python33\lib\site-packages\pandas\computation\expressions.py in _evaluate_standard(op, op_str, a, b, raise_on_error, **eval_kwargs)
     70         _store_test_result(False)
---> 71     return op(a, b)
     72

c:\python33\lib\site-packages\pandas\core\ops.py in _radd_compat(left, right)
    805     try:
--> 806         output = radd(left, right)
    807     except TypeError:

c:\python33\lib\site-packages\pandas\core\ops.py in <lambda>(x, y)
    802 def _radd_compat(left, right):
--> 803     radd = lambda x, y: y + x
    804     # GH #353, NumPy 1.5.1 workaround

TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('<U32') dtype('<U32') dtype('<U32')

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-34-3b2873f8749b> in <module>()
----> 1 df.loc[df['col1']== 'John','col3'] = 'I am ' + df['col2']

c:\python33\lib\site-packages\pandas\core\ops.py in wrapper(left, right, name, na_op)
    616                 lvalues = lvalues.values
    617
--> 618             return left._constructor(wrap_results(na_op(lvalues, rvalues)),
    619                                      index=left.index, name=left.name,
    620                                      dtype=dtype)

c:\python33\lib\site-packages\pandas\core\ops.py in na_op(x, y)
    561                 result = np.empty(len(x), dtype=x.dtype)
    562                 mask = notnull(x)
--> 563                 result[mask] = op(x[mask], y)
    564             else:
    565                 raise TypeError("{typ} cannot perform the operation {op}".format(typ=type(x).__name__,op=str_rep))

c:\python33\lib\site-packages\pandas\core\ops.py in _radd_compat(left, right)
    804     # GH #353, NumPy 1.5.1 workaround
    805     try:
--> 806         output = radd(left, right)
    807     except TypeError:
    808         raise

c:\python33\lib\site-packages\pandas\core\ops.py in <lambda>(x, y)
    801
    802 def _radd_compat(left, right):
--> 803     radd = lambda x, y: y + x
    804     # GH #353, NumPy 1.5.1 workaround
    805     try:

TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('<U32') dtype('<U32') dtype('<U32')

如果Pandas不喜欢Column值==某些文本,如果所有值都是NaN ????

帮助!

2 个答案:

答案 0 :(得分:1)

我认为,如果有任何非空的值,那么这一行所做的就是将字符串添加到第1列值。

df.loc[df['col1'].notnull(),'col3'] = 'I am ' + df['col1']

因此,您可以检查是否存在任何非空值,然后仅在执行以下操作时执行操作:

if df['col1'].notnull().any():
    df['col3'] = 'I am ' + df['col1']

在以这种方式运行之前,您也不需要创建col3列。

答案 1 :(得分:0)

这里的问题是,如果整个列是np.nan,它可能存储为浮点数,而不是对象(文本)。

所以你可以这样做:

if not np.all(pandas.isnull(df['mycol'])):
    df = my_string_operation(df)

您还可以将相关列强制转换为object类型。

df['mycol'] = df['mycol'].astype(object)
df = my_string_operation(df)