I have following df:
data = [
['a', 'one', 10],
['a', 'two', 10],
['b', 'one', 13],
['b', 'two', 100],
['c', 'two', 100],
['c', 'one', 100],
['d', 'one', 100],
['d', 'one', 10
]
df = pd.DataFrame(data, columns=['key1', 'key2', 'key3'])
key1 key2 key3
0 a one 10
1 a two 10
2 b one 13
3 b two 100
4 c two 100
5 c one 100
6 d one 100
7 d one 10
I need to group by key1
,
- search for
key2 == 'one'
(for single occurrence result = OK, for 0 or >1 = NOK), - also additionally check if
key3 == 10
or100
then OK, anything else is NOK.
As result I should have OK
or NOK
. How can I get something like the following as output?
key1 result
a ok
b nok
c ok
d nok
I found a similar question and answer here but without additional check for key3
. What I have so far is the following:
test = df.groupby('key1')['key2'].apply(lambda x:(x=='one').sum()).reset_index(name='result')
test['result'].where(~(test['result'] > 1), other='nok', inplace=True)
test['result'].replace([0, 1], ['nok', 'ok'], inplace = True)
When I run this code, it gives me following output:
key1 result
0 a ok
1 b ok
2 c ok
3 d nok
How can I add the check for key3
?