Assume there are many columns in a data frame that are of string type but always have a value of “N” or “Y”. You would like to scan a column to determine if this is true and if it is really just Y or N, then you might want to change the column type to boolean and have false/true as the values of the cells.
What the Below Code does:
1. Collects the Column Names and Column Types in a Python List
2. Iterate over a for loop and collect the distinct value of the columns in a two dimensional array
3. In the Loop, check if the Column type is string and values are either ‘N’ or ‘Y’
4. If Yes ,Convert them to Boolean and Print the value as true/false Else Keep the Same type.
See the Output:
I have attached the snapshot of the results. Please look at the columns flag1,flag3 converted to true or false and rest of the columns are printed with the original value.