pandas:根据多个其他列的值创建一个新列

时间:2017-03-21 00:51:12

标签: python-3.x pandas lambda apply

我有以下pandas数据框src

my_df

我想根据以下条件添加新的col_A col_B ------------------- blue medium red small yellow big

col_C

为实现这一目标,我做了以下工作:

if col_A == 'blue', col_C = 'A_blue'
if col_B == 'big', col_C = 'B_big'

For all other cases, col_C = ''

但是我收到了以下错误:

def my_bad_data(row):
    if row['col_A'] == 'blue':
        return 'A_blue'
    elif row['col_B'] == 'big':
        return 'B_big'
    else:
        return ''

my_df['col_C'] = my_df.apply(lambda row: my_bad_data(row))

知道我在这里做错了吗?谢谢!

1 个答案:

答案 0 :(得分:1)

是的,我经常遇到这种情况,你想要$sql= "SELECT * FROM staff WHERE BusinessID = $BusinessID"; 。请参阅文档here

dataframe.apply(func, axis=1)