“Pandas Set Kondisi Multi Kolom” Kode Jawaban

Pandas Set Kondisi Multi Kolom

conditions = [
   .....:     (df['col2'] == 'Z') & (df['col1'] == 'A'),
   .....:     (df['col2'] == 'Z') & (df['col1'] == 'B'),
   .....:     (df['col1'] == 'B')
   .....: ]
   .....: 

In [214]: choices = ['yellow', 'blue', 'purple']

In [215]: df['color'] = np.select(conditions, choices, default='black')

In [216]: df
Out[216]: 
  col1 col2   color
0    A    Z  yellow
1    B    Z    blue
2    B    X  purple
3    C    Y   black
NP

Pandas Set Kondisi Multi Kolom

conditions = [
            (df['col2'] == 'Z') & (df['col1'] == 'A'),
            (df['col2'] == 'Z') & (df['col1'] == 'B'),
            (df['col1'] == 'B')
			]


choices = ['yellow', 'blue', 'purple']

df['color'] = np.select(conditions, choices, default='black')

df
# Out[216]: 
#  col1 col2   color
# 0    A    Z  yellow
# 1    B    Z    blue
# 2    B    X  purple
# 3    C    Y   black
NP

Jawaban yang mirip dengan “Pandas Set Kondisi Multi Kolom”

Pertanyaan yang mirip dengan “Pandas Set Kondisi Multi Kolom”

Lebih banyak jawaban terkait untuk “Pandas Set Kondisi Multi Kolom” di Python

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya