简体   繁体   中英

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?

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'})

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

My Take

cols = df.columns[df.dtypes.eq(bool)]
vals = np.column_stack([df[c].values for c in cols])

df[cols] = np.array(['No', 'Yes'])[vals.astype(int)]

df

   A  B    C    D
0  0  x  Yes   No
1  1  y   No  Yes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM