在数据框中,我想计算一个额外的列“desired_output”(默认的“期望列”是123456),它组合了另外6列的值(c_flow 1,c_flow 2,c_flow 3,c_flow 4,c_flow 5, c_flow 6)如下:如果第1列中的行= 0.0且第4列= 0.0,则“desired_output”变为:x23x56。
“try1”是我现在可以输出的内容。
c_flow_1 = [1, 20, 0, 3, 0, 2]
c_flow_2 = [10, 20, 5, 10, 0, 0]
c_flow_3 = [10, 20, 0, 10, 1, 2]
c_flow_4 = [0, 20, 0, 10, 1, 2]
c_flow_5 = [10, 0, 1, 10, 1, 5]
c_flow_6 = [10, 0, 0, 10, 1, 2]
desired_output = ['123x56', '1234xx', 'x2xx5x', '123456','xx3456','1x3456']
data = pd.DataFrame({'c_flow 1': c_flow_1,
'c_flow 2': c_flow_2,
'c_flow 3': c_flow_3,
'c_flow 4': c_flow_4,
'c_flow 5': c_flow_5,
'c_flow 6': c_flow_6,
'desired_output': desired_output
})
conditions = [data['c_flow 1'] == 0, data['c_flow 2'] == 0, data['c_flow 3'] == 0,
data['c_flow 4'] == 0, data['c_flow 5'] == 0, data['c_flow 6'] == 0 ]
choices = ['x23456', '1x3456', '12x456', '123x56', '1234x6', '12345x']
data['try1'] = np.select(conditions, choices, default ='123456')
答案 0 :(得分:4)
让我们用一点数学来简化事情。
DataFrame.filter
来执行此操作DataFrame.mul
x
DataFrame.replace
DataFrame.agg
(data.filter(like='c_flow')
.gt(0)
.mul(range(1, 7))
.replace(0, 'x')
.astype(str)
.agg(''.join, 1)
)
0 123x56
1 1234xx
2 x2xx5x
3 123456
4 xx3456
5 1x3456
dtype: object
答案 1 :(得分:1)
我尝试了这个(我知道这不是有效的),但仍然张贴以获得一个想法
def cust(row):
t=[]
i=1
for val in row:
if val!=0:
t.append(str(i))
else:
t.append('x')
i+=1
return ''.join(t)
print df.apply(cust,axis=1)