Python loop over values in dataframe and change to binary values

后端 未结 2 583
隐瞒了意图╮
隐瞒了意图╮ 2021-01-21 06:40

I have a series y in Python with values Accepted and Rejected. I want to create a new dataframe with value 1 for Ac

2条回答
  •  感情败类
    2021-01-21 07:08

    you can use:

    df['dummy'] = df.y.apply(lambda x:  1 if  x == 'Accepted' else 0)
    

    if you want to use a for loop:

    new_dummy_data = []
    
    for value in df.y.values:
        if value == 'Accepted':
            new_dummy_data.append(1)
        else:
            new_dummy_data.append(0)
    
    df['dummy'] = new_dummy_data
    

提交回复
热议问题