我想基于其他列(X,Y)中的值设置熊猫数据框中的列(Z)的值:
以下是示例代码:
for i, row in df.iterrows():
#print(i, row['Z'])
if row['X'] == 1 and row['Y'] == 0:
row['Z'] = 1
if row['X'] == 0 and row['Y'] == 1:
row['Z'] = 0
if row['X'] == 0 and row['Y'] == 0:
row['Z'] = 2
if row['X'] == 1 and row['Y'] == 1:
row['Z'] = 3
这样做的方式是什么?
答案 0 :(得分:1)
将numpy.select
与&
一起用于按位AND
:
m1 = df['X'] == 0
m2 = df['X'] == 1
m3 = df['Y'] == 0
m4 = df['Y'] == 1
df['Z'] = np.select([m2 & m3, m1 & m4, m1 & m3, m2 & m4], [1,0,2,3])
答案 1 :(得分:0)
如果只想编码Z值,则可以解决
df['Z'] = df['X'] + 2 * df['Y']
如果不是这种情况,请在之后使用pandas.Series.map
>
df['Z'] = df['Z'].map({1:0, 2:1, 0:2, 3:3})