Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map True and False to 'Yes' and 'No' in a pandas data frame for columns of dtype bool only?

I have a pandas data frame (v 0.20.3):

df = pd.DataFrame({'coname1': ['Apple','Yahoo'], 'coname2':['Apple', 'Google']})
df['eq'] = df.apply(lambda row: row['coname1'] == row['coname2'], axis=1).astype(bool)

   coname1 coname2     eq
0    Apple   Apple   True
1    Yahoo  Google  False

If I would like to replace True/False to 'Yes'/'No', I could run this:

df.replace({
                True: 'Yes',
                False: 'No'
            })

   coname1 coname2   eq
0    Apple   Apple  Yes
1    Yahoo  Google   No

Which seems to get the job done. However, if a data frame is just one row with a value of 0/1 in a column, it will be also replaced as it's being treated as Boolean.

df1 = pd.DataFrame({'coname1': [1], 'coname2':['Google'], 'coname3':[777]})
df1['eq'] = True

   coname1 coname2  coname3    eq
0        1  Google      777  True

df1.replace({
                True: 'Yes',
                False: 'No'
            })

  coname1 coname2 coname3   eq
0     Yes  Google     777  Yes

I would like to map True/False to Yes/No for all columns in the data frame that are of dtype bool.

How do I tell pandas to run map True/False to arbitrary strings only for the columns that are of dtype bool without explicitly specifying the names of columns as I may not know them in advance?

like image 352
Alex Tereshenkov Avatar asked Jul 19 '17 16:07

Alex Tereshenkov


2 Answers

Use the dtypes attribute to check if the column is boolean and filter based on that:

df = pd.DataFrame({'A': [0, 1], 'B': ['x', 'y'], 
                   'C': [True, False], 'D': [False, True]})

df
Out: 
   A  B      C      D
0  0  x   True  False
1  1  y  False   True

bool_cols = df.columns[df.dtypes == 'bool']

df[bool_cols] = df[bool_cols].replace({True: 'Yes', False: 'No'})

df
Out: 
   A  B    C    D
0  0  x  Yes   No
1  1  y   No  Yes

I think the fastest way would be to use map in a loop though:

for col in df.columns[df.dtypes == 'bool']:
    df[col] = df[col].map({True: 'Yes', False: 'No'})
like image 93
ayhan Avatar answered Sep 27 '22 00:09

ayhan


A nice workaround is to create a function that first checks if the element is of type bool or not, and then use applymap:

import pandas as pd

df1 = pd.DataFrame({'coname1': [1], 'coname2':['Google'], 'coname3':[777]})
df1['eq'] = True

def bool2yes(boolean):
    if isinstance(boolean, bool):
        if boolean == True:
            return "Yes"
        else:
            return "No"
    else:
        return boolean

>>> df1.applymap(bool2yes)
   coname1 coname2  coname3   eq
0        1  Google      777  Yes
like image 26
Vinícius Figueiredo Avatar answered Sep 25 '22 00:09

Vinícius Figueiredo