按多个条件添加新列

时间:2018-06-17 03:55:14

标签: python python-3.x pandas

我正在尝试按现有类别category添加新列id

conditions = [
    (result['id'] == 1362) or (result['id'] == 7463),
    (result['id'] == 543) or (result['id'] == 3424)]
choices = ['A1', 'A2']
result['category'] = np.select(conditions, choices, default='black')

但是,我收到了一个错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-289-7cb2bbdaab53> in <module>()
      1 conditions = [
----> 2     (result['id'] == 1362) or (result['id'] == 7463),
      3     (result['id'] == 543) or (result['id'] == 3424)]
      4 choices = ['A1', 'A2']
      5 result['category'] = np.select(conditions, choices, default='black')

/anaconda/lib/python3.6/site-packages/pandas/core/generic.py in __nonzero__(self)
    951         raise ValueError("The truth value of a {0} is ambiguous. "
    952                          "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
--> 953                          .format(self.__class__.__name__))
    954 
    955     __bool__ = __nonzero__

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

我该如何纠正?

1 个答案:

答案 0 :(得分:0)

使用pandas,您需要使用元素逻辑或|np.logical_or()

import numpy as np
import pandas as pd

d = {"id": [1362, 1361, 7463, 7462, 543, 542, 3424, 3333]}
result = pd.DataFrame(d)

conditions = [np.logical_or(result['id'] == 7463, result['id'] == 1362),
              np.logical_or(result['id'] == 3424, result['id'] == 543)]

#Alternate syntax
#conditions = [(result['id'] == 7463) | (result['id'] == 1362),
#              (result['id'] == 3424) | (result['id'] == 543)]

choices = ['A1', 'A2']
result['category'] = np.select(conditions, choices, default='black')

print(result)

     id category
0  1362       A1
1  1361    black
2  7463       A1
3  7462    black
4   543       A2
5   542    black
6  3424       A2
7  3333    black