[英]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'.我的目标是从列选择“RT”、“RW”、“Kelurahan”中创建一个名为“Status Zonasi”的新列,其值为 ['Zonasi 1', 'Zonasi 2', 'Zonasi 3', 'Belum Diketahui'] ','贾鲁尔达夫塔'。
When using list in the condition, you should use 'in'.在条件中使用列表时,应使用'in'。 Ex.前任。 w in [0,1,2...] w 在 [0,1,2...]
Ensure the datatypes of the columns are correct.确保列的数据类型正确。
Finally, I don't see any row which meets the condition for 'zonasi 1' and 'zonasi 2' from your sample df.最后,我从您的示例 df 中看不到任何符合“zonasi 1”和“zonasi 2”条件的行。 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'如果上述 2 没有帮助,您能否分享您的数据的另一个子集,包括您希望 'status_zone' 为 'zonasi 1 或 2' 的一些行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.