Добавьте еще один столбец на основе значения двух столбцов

#python #pandas #numpy

Вопрос:

Я пытаюсь добавить еще один столбец, основанный на значении двух столбцов. Вот мини-версия моего фрейма данных.

 data = {'current_pair': ['"["StimusNeu/2357.jpg","StimusNeu/5731.jpg"]"', '"["StimusEmo/6350.jpg","StimusEmo/3230.jpg"]"', '"["StimusEmo/3215.jpg","StimusEmo/9570.jpg"]"','"["StimusNeu/7020.jpg","StimusNeu/7547.jpg"]"', '"["StimusNeu/7080.jpg","StimusNeu/7179.jpg"]"'],
        'B': [1, 0, 1, 1, 0]
        }
df = pd.DataFrame(data)
df

                                    current_pair    B
0   "["StimusNeu/2357.jpg","StimusNeu/5731.jpg"]"   1
1   "["StimusEmo/6350.jpg","StimusEmo/3230.jpg"]"   0
2   "["StimusEmo/3215.jpg","StimusEmo/9570.jpg"]"   1
3   "["StimusNeu/7020.jpg","StimusNeu/7547.jpg"]"   1
4   "["StimusNeu/7080.jpg","StimusNeu/7179.jpg"]"   0
 

Я хочу, чтобы результат был:

                                     current_pair    B   C
0   "["StimusNeu/2357.jpg","StimusNeu/5731.jpg"]"   1   1
1   "["StimusEmo/6350.jpg","StimusEmo/3230.jpg"]"   0   2
2   "["StimusEmo/3215.jpg","StimusEmo/9570.jpg"]"   1   0
3   "["StimusNeu/7020.jpg","StimusNeu/7547.jpg"]"   1   1
4   "["StimusNeu/7080.jpg","StimusNeu/7179.jpg"]"   0   2
 

Я использовал команды numpy select:

 conditions=[(data['B']==1 amp; data['current_pair'].str.contains('Emo/', na=False)),
            (data['B']==1 amp; data['current_pair'].str.contains('Neu/', na=False)),
            data['B']==0]
choices = [0, 1, 2]
data['C'] = np.select(conditions, choices, default=np.nan)
 

К сожалению, он выдает мне этот кадр данных, не распознав ничего с «1» в столбце «C».

                                     current_pair    B   C
0   "["StimusNeu/2357.jpg","StimusNeu/5731.jpg"]"   1   0
1   "["StimusEmo/6350.jpg","StimusEmo/3230.jpg"]"   0   2
2   "["StimusEmo/3215.jpg","StimusEmo/9570.jpg"]"   1   0
3   "["StimusNeu/7020.jpg","StimusNeu/7547.jpg"]"   1   0
4   "["StimusNeu/7080.jpg","StimusNeu/7179.jpg"]"   0   2
 

Любая помощь имеет значение! большое спасибо.

Ответ №1:

Существует проблема с () after ==1 для приоритета операторов:

 conditions=[(data['B']==1) amp; data['current_pair'].str.contains('Emo/', na=False),
            (data['B']==1) amp; data['current_pair'].str.contains('Neu/', na=False),
             data['B']==0]
 

Ответ №2:

Я думаю, что здесь какая-то логика пошла не так; это работает:

 df.assign(C=np.select([df.B==0, df.current_pair.str.contains('Emo/'), df.current_pair.str.contains('Neu/')], [2,0,1]))
 

Ответ №3:

Вот несколько более обобщенное предложение, легко применимое к более сложным случаям. Однако вам следует помнить о скорости выполнения:

 import pandas as pd
df = pd.DataFrame({'col_1': ['Abc', 'Xcd', 'Afs', 'Xtf', 'Aky'], 'col_2': [1, 2, 3, 4, 5]})
def someLogic(col_1, col_2):
    if 'A' in col_1 and col_2 == 1:
        return 111
    elif "X" in col_1 and col_2 == 4:
        return 999
    return 888
df['NewCol'] = df.apply(lambda row: someLogic(row.col_1, row.col_2), axis=1, result_type="expand")
print(df)