简体   繁体   English

通过多个条件添加新列

[英]Add a new column by multiple conditions

I am trying to add a new column, category , by the existing category's id . 我试图通过现有类别的id添加一个新列category

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')

But, I got an error: 但是,我遇到了一个错误:

---------------------------------------------------------------------------
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().

How can I correct this? 我该如何纠正?

With pandas you need to use elementwise logical or | 对于pandas您需要使用元素级逻辑或| or np.logical_or() : 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM