Pandas:当数据为NaN时逻辑运算不能为don

2024-04-20 10:53:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Pandas中有一个大的数据帧,2列可以有值,或者当没有分配给任何值时是NaN(Null)。在

我想根据这两个填充第三列。当不是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

这也适用于:

^{pr2}$

但是如果我没有将所有值设为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不喜欢列value==some text如果所有值都是NaN????在

救命啊!在


Tags: inpypandasdflibpackagesnpsite
2条回答

这里的问题是,如果整个列是np.nan,那么它可能存储为float,而不是object(文本)。在

所以你可以:

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

您还可以将有问题的列强制为object类型。在

^{pr2}$

我认为这行代码所做的就是在列1的值中添加一个字符串,如果有任何值不是null的话。在

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

因此,您只需检查是否有非空值,然后只在存在以下值时执行该操作:

^{pr2}$

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

相关问题 更多 >