'Decode all values in a categorical column of pandas data-frame by a specific rule [closed]

decode categorical data

Only B and D values are to be decoded as 0; rest of the values would be decoded as 1.

A set of values are decoded as 0 and rest of the values are decoded as 1; may be a binary decoding.



Solution 1:[1]

IIUC, you can use:

df['pulse'] = (~df['fault_code'].isin(['B', 'D'])).astype(int)

or taking advantage of boolean -> int equivalence:

df['pulse'] = df['fault_code'].isin(['B', 'D']).rsub(1)

output:

no output as no text data was provided

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mozway