P1—Pn任意一列值为“1”时,要求final_result为“1”,否则final_result为“0”
表1:class P1 P2 P3 … Pn final_result
A 0 1 0 0 0 ?
B 0 0 0 0 0 ?
C 0 0 0 1 0 ?
D 1 0 1 0 0 ?
E 0 0 0 0 0 ?
F 0 0 0 0 1 ?
要求:P1—Pn任意一列值为“1”时,要求final_result为“1”,否则final_result为“0”。
最终输出:
class P1 P2 P3 … Pn final_result
A 0 1 0 0 0 1
B 0 0 0 0 0 0
C 0 0 0 1 0 1
D 1 0 1 0 0 1
E 0 0 0 0 0 0
F 0 0 0 0 1 1
from functools import reduce
data = [,
,
,
,
,
]
for each in data:
print(reduce(lambda x, y: x or y, each))
1
0
1
1
0
1 import pandas as pd
data = pd.DataFrame([['A', '0', '1', '0', '0', '0', None],
['B', '0', '0', '0', '0', '0', None],
['C', '0', '0', '0', '1', '0', None]],
columns=['class', 'P1', 'P2', 'P3', 'P4', 'Pn', 'final_result'])
print(data)
data['final_result']=data.apply(lambda x:'1' if '1' in x.values else '0',axis=1)
print(data)
class P1 P2 P3 P4 Pn final_result
0 A01000 None
1 B00000 None
2 C00010 None
class P1 P2 P3 P4 Pn final_result
0 A01000 1
1 B00000 0
2 C00010 1
页:
[1]