简体   繁体   English

从 4 个现有列创建一个新列(python pandas)

[英]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'] ','贾鲁尔达夫塔'。

this is a sample from my df这是我的 df 的样本

  1. When using list in the condition, you should use 'in'.在条件中使用列表时,应使用'in'。 Ex.前任。 w in [0,1,2...] w 在 [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.最后,我从您的示例 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.

相关问题 从 3 个现有列创建一个新列(python pandas) - create a new column from 3 existing columns (python pandas) 使用 python pandas 从现有列创建一个新的地图列 - Create a new map column from existing columns using python pandas Python:从现有列创建一个新列 - Python: create a new column from existing columns 使用 Pandas 从现有列创建新列到数据框 - Create a new column to data frame from existing columns using Pandas 如何基于从python / pandas中现有列派生的列表创建新列? - How do I create new columns based off of a list derived from an existing column in python/pandas? 使用 pandas/python 从 DataFrame 中的两个现有文本列创建一个新列 - Create a new column from two existing text columns in a DataFrame using pandas/python python pandas dataframe从其他列的单元格创建新列 - python pandas dataframe create new column from other columns' cells Python pandas 从两列创建新的字典列 - Python pandas create new dict column from two columns 根据Python中Pandas数据框现有列的值创建新列的最佳方法是什么? - What is the best way to create new columns based on value of existing column of Pandas dataframe in Python? 从python中的现有列创建新列 - Creating new columns from existing column in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM