简体   繁体   English

使用 if 状态级联多个条件,以创建 2 个分类列

[英]Cascading Multiple conditions with if status, to create 2 categorical columns

Well It was been like half an hour writting and rewritting this with no success: Hope any caritative soul can help me with some wisdom嗯,写了又改写了半个小时,但没有成功:希望任何有爱心的人都可以帮助我一些智慧

Heres the two pieces of code I have tried, the most basic ones I tried before did not work also: I am triying to make 2 columns with this if else conditional, I cant really figure out whats going on...这是我尝试过的两段代码,我之前尝试过的最基本的代码也不起作用:如果其他条件有条件,我正在尝试用这个制作 2 列,我无法真正弄清楚发生了什么......

vel_sign = []
ac_sign = []
for i in range(len(X_train)):
    if (int(X_train['Velocidad'][i]) > 0) and (int(X_train['Aceleracion'][i]) > 0):
        vel_sign.append(1)     
        ac_sign.append(1)
    elif (int(X_train['Velocidad'][i]) > 0) and (int(X_train['Aceleracion'][i]) == 0):
        vel_sign.append(1)
        ac_sign.append(0)
    elif (int(X_train['Velocidad'][i]) > 0) and (int(X_train['Aceleracion'][i]) < 0):
        vel_sign.append(1)
        ac_sign.append(-1)
    else: pass

vel_sign = []
ac_sign = []

for i in range(len(X_train)):(
    if (X_train['Velocidad'][i] > 0):
        vel_sign.append(1)        
        if  (X_train['Aceleracion'][i] > 0): 
            ac_sign.append(1)
        elif (X_train['Aceleracion'][i] == 0): 
            ac_sign.append(0)
        elif (X_train['Aceleracion'][i] < 0):
            ac_sign.append(-1)
        else:pass
    elif (X_train['Velocidad'][i] == 0):
        vel_sign.append(0)
        if  (X_train['Aceleracion'][i] > 0): 
            ac_sign.append(1)
        elif (X_train['Aceleracion'][i] == 0): 
            ac_sign.append(0)
        elif (X_train['Aceleracion'][i] < 0):
            ac_sign.append(-1)
        else: pass
    elif (X_train['Velocidad'][i] < 0):
        vel_sign.append(-1)
        if  (X_train['Aceleracion'][i] > 0): 
            ac_sign.append(1)
        elif (X_train['Aceleracion'][i] == 0): 
            ac_sign.append(0)
        elif (X_train['Aceleracion'][i] < 0):
            ac_sign.append(-1)
        else: pass
    else: pass)

X_train['V_signo'] = vel_sign
X_train['A_signo'] = ac_sign
print(X_train.head())

The error is the following错误如下

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-39-d5c1cf0b654a> in <module>
      2 ac_sign = []
      3 for i in range(len(X_train)):
----> 4     if (int(X_train['Velocidad'][i]) > 0) and (int(X_train['Aceleracion'][i]) > 0):
      5         vel_sign.append(1)
      6         ac_sign.append(1)

/opt/conda/lib/python3.6/site-packages/pandas/core/series.py in __getitem__(self, key)
   1069         key = com.apply_if_callable(key, self)
   1070         try:
-> 1071             result = self.index.get_value(self, key)
   1072 
   1073             if not is_scalar(result):

/opt/conda/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_value(self, series, key)
   4728         k = self._convert_scalar_indexer(k, kind="getitem")
   4729         try:
-> 4730             return self._engine.get_value(s, k, tz=getattr(series.dtype, "tz", None))
   4731         except KeyError as e1:
   4732             if len(self) > 0 and (self.holds_integer() or self.is_boolean()):

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

KeyError: 3

THANKS!!!!!!!!!!!!!谢谢!!!!!!!!!!!!!

I think I got an Answer, But if anyone got a better one, I would like to hear it :D:D我想我得到了答案,但如果有人有更好的答案,我想听听 :D:D

def Signo(array):
    result = []
    for x in range(len(array)):
        if array[x] > 0:
            result.append(1)
        elif array[x] == 0:
            result.append(0)
        else:
            result.append(-1)
    return result

df['Vel_signo'] = Signo(df['Velocidad'].to_numpy())
df['Ac_signo'] = Signo(df['Aceleracion'].to_numpy())

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

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