Python 有条件创建dataframe列:基于多个条件

Python 有条件创建dataframe列:基于多个条件,python,pandas,numpy,dataframe,Python,Pandas,Numpy,Dataframe,我有一个df: col1 col2 col3 0 1 2 3 1 2 3 1 2 3 3 3 3 4 3 2 我想根据以下条件添加一个新列: - if col1 > col2 > col3 -----> 2 - elif col1 > col2 -----> 1 - elif col1 < col2 < col3 -----> -2

我有一个df:

  col1 col2 col3
0    1    2    3
1    2    3    1
2    3    3    3
3    4    3    2
我想根据以下条件添加一个新列:

 - if   col1 > col2 > col3   ----->  2
 - elif col1 > col2          ----->  1
 - elif col1 < col2 < col3   -----> -2
 - elif col1 < col2          -----> -1
 - else                      ----->  0
我遵循的方法是,中的1大于或小于就可以了。但在我的情况下,大于1大于或小于,条件返回错误:

conditions = [
       (df['col1'] > df['col2'] > df['col3']), 
       (df['col1'] > df['col2']),
       (df['col1'] < df['col2'] < df['col3']),
       (df['col1'] < df['col2'])]
choices = [2,1,-2,-1]
df['new'] = np.select(conditions, choices, default=0)


Traceback (most recent call last):

  File "<ipython-input-43-768a4c0ecf9f>", line 2, in <module>
    (df['col1'] > df['col2'] > df['col3']),

  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py", line 1478, in __nonzero__
    .format(self.__class__.__name__))

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
条件=[
(df['col1']>df['col2']>df['col3']),
(df['col1']>df['col2']),
(df['col1']df['col2']>df['col3']),
文件“C:\ProgramData\Anaconda3\lib\site packages\pandas\core\generic.py”,第1478行,非零__
.format(self.\uuuuuu class.\uuuuuuu.\uuuuuu name.\uuuuuuuuu))
ValueError:序列的真值不明确。使用a.empty、a.bool()、a.item()、a.any()或a.all()。
我应该怎么做?

将代码更改为

conditions = [
       (df['col1'] > df['col2']) &  (df['col2'] > df['col3']), 
       (df['col1'] > df['col2']),
       (df['col1'] < df['col2']) & (df['col2'] < df['col3']),
       (df['col1'] < df['col2'])]
choices = [2,1,-2,-1]
df['new'] = np.select(conditions, choices, default=0)
条件=[
(df['col1']>df['col2'])和(df['col2']>df['col3']),
(df['col1']>df['col2']),
(df['col1']
尝试使用“.eval”方法创建单个布尔序列,例如,
df.eval('col1>col2>col3')
。如果一个元素接一个元素,col1大于col2,而col2大于col3,则这相当于指定True。否则,pandas会为
df['col1']>df['col2']
创建一个布尔序列,然后它不知道如何计算条件
boolean series>df['col3']
conditions = [
       (df['col1'] > df['col2']) &  (df['col2'] > df['col3']), 
       (df['col1'] > df['col2']),
       (df['col1'] < df['col2']) & (df['col2'] < df['col3']),
       (df['col1'] < df['col2'])]
choices = [2,1,-2,-1]
df['new'] = np.select(conditions, choices, default=0)