简体   繁体   中英

create a new column from 4 existing columns (python pandas)

status_zona = []
for w,x,y,z in zip(df_51['RT'], df_51['RW'], df_51['Kelurahan'], df_51['Jalur Daftar']):
    if w == 12 and x == 2 and y == 'Batu Ampar':
        c = 'Zonasi 1'
    elif w == 15 and x == 5 and y == 'Batu Ampar':
        c = 'Zonasi 1'
    elif w == 7 and x == 4 and y == 'Batu Ampar':
        c = 'Zonasi 1'
    elif w == 11 and x == 4 and y == 'Batu Ampar':
        c = 'Zonasi 1'
    elif w == 11 and x == 2 and y == 'Batu Ampar':
        c = 'Zonasi 1'
    elif w == 10 and x == 2 and y == 'Batu Ampar':
        c = 'Zonasi 1'
    elif w == 9 and x == 2 and y == 'Batu Ampar':
        c = 'Zonasi 1'
    elif w == 14 and x == 2 and y == 'Batu Ampar':
        c = 'Zonasi 1'
    elif w == 1 and x == 4 and y == 'Batu Ampar':
        c = 'Zonasi 2'
    elif w == 9 and x == 3 and y == 'Batu Ampar':
        c = 'Zonasi 2'
    elif w == 6 and x == 5 and y == 'Batu Ampar':
        c = 'Zonasi 2'
    elif w == 5 and x == 5 and y == 'Batu Ampar':
        c = 'Zonasi 2'
    elif w == 8 and x == 2 and y == 'Batu Ampar':
        c = 'Zonasi 2'
    elif w == 15 and x == 2 and y == 'Batu Ampar':
        c = 'Zonasi 2'
    elif w == 17 and x == 2 and y == 'Batu Ampar':
        c = 'Zonasi 2'
    elif w == 1 and x == 6 and y == 'Batu Ampar':
        c = 'Zonasi 2'
    elif w == 10 and x == 4 and y == 'Batu Ampar':
        c = 'Zonasi 2'
    elif w == 13 and x == 4 and y == 'Batu Ampar':
        c = 'Zonasi 2'
    elif w == 4 and x == 4 and y == 'Batu Ampar':
        c = 'Zonasi 2'
    elif w == 6 and x == 4 and y == 'Batu Ampar':
        c = 'Zonasi 2'
    elif w == 12 and x == 4 and y == 'Batu Ampar':
        c = 'Zonasi 2'
    elif w == [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18] and x == [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18] \
    and y == ['Batu Ampar'] and z == ['Zonasi', 'Afirmasi (KJP)']:
        c = 'Zonasi 3'
    elif w == [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18] and x == [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18] \
    and y == ['Batu Ampar', 'Bale Kambang', 'Pejaten Barat', 'Pejaten Timur', 'Cawang', 'Cililitan', 'Gedong', 'Rawajati', 
              'Kalibata', 'Kramat Jati', 'Tengah'] and z == ['Afirmasi (inklusi)', 'Prestasi (Akademik)', 'Prestasi (Non Akademik)', 'Afirmasi (Anak Panti)', 'Tahap Kedua']:
        c = 'Belum Diketahui'
    else:
        c = 'error'
    status_zona.append(c)

I think my code is correct, but why does the result show an error? there should be no error value at all. My goal is to create a new column named 'Status Zonasi' with values ['Zonasi 1', 'Zonasi 2', 'Zonasi 3', 'Belum Diketahui'] from the column selection 'RT', 'RW', 'Kelurahan', 'Jalur Daftar'.

this is a sample from my df

  1. When using list in the condition, you should use 'in'. Ex. w in [0,1,2...]

  2. Ensure the datatypes of the columns are correct.

  3. Finally, I don't see any row which meets the condition for 'zonasi 1' and 'zonasi 2' from your sample df. If the above 2 doesn't help, could you share another subset of your data including some rows where you expect 'status_zone' to be 'zonasi 1 or 2'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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