How to Map True/False to 1/0 in a Pandas Dataframe

How can I map True/False to 1/0 in a Pandas DataFrame?

A succinct way to convert a single column of boolean values to a column of integers 1 or 0:

df["somecolumn"] = df["somecolumn"].astype(int)

Converting true/false to 0/1 boolean in a mixed dataframe

Let's modify your lambda to use an isinstance check:

df.applymap(lambda x: int(x) if isinstance(x, bool) else x)

Only values of type bool will be converted to int, everything else remains the same.


As a better solution, if the column types are scalar (and not "mixed" as I originally assumed given your question), you can instead use

u = df.select_dtypes(bool)
df[u.columns] = u.astype(int)

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

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

how to map 1 and 0 for all true and false

Use:

splitsE = (df.COLUMN_1.str.contains(' Each')) & (df.COLUMN_2.str.contains(' EACH'))

I think simpliest is convert boolean mask to integer for True/False to 1/0 map:

df['CASE#'] = splitsE.astype(int)

Another solution is use numpy.where:

df['CASE#'] = np.where(splitsE, 1, 0)

Or map with dictionary with removed one ():

df['CASE#'] = splitsE.map({True:'1', False:'0'})

EDIT: For another condition use numpy.select:

mask1 = df.COLUMN_3.str.contains('EACH', case=False, na=False)
mask2 = df.COLUMN_3.str.contains('/', case=False, na=False)

df['CASE#'] = np.select([mask1, mask2], [1, 2], default=0)
print (df)
COLUMN_3 CASE#
0 25/PACK 2
1 EACH 1
2 100/BOTTLE 2
3 25/PACK 2
4 NaN 0
5 3/PACK 2
6 EACH 1

Pandas efficiently add new column true/false if between two other columns

Like mentioned by @Brebdan, you can use this builtin:

test["between"] = test["x"].between(test["low"], test["high"])

output:

    fid x   low high    between
0 0 0.18 0.1 0.2 True
1 1 0.07 0.1 0.2 False
2 2 0.11 0.1 0.2 True
3 3 0.30 0.1 0.2 False
4 4 0.33 0.1 0.2 False

Pandas mapping to TRUE/FALSE as String, not Boolean

If need replace boolean values True and False:

booleandf = pandasDF.select_dtypes(include=[bool])
booleanDictionary = {True: 'TRUE', False: 'FALSE'}

for column in booleandf:
pandasDF[column] = pandasDF[column].map(booleanDictionary)

Sample:

pandasDF = pd.DataFrame({'A':[True,False,True],
'B':[4,5,6],
'C':[False,True,False]})

print (pandasDF)
A B C
0 True 4 False
1 False 5 True
2 True 6 False

booleandf = pandasDF.select_dtypes(include=[bool])
booleanDictionary = {True: 'TRUE', False: 'FALSE'}

#loop by df is loop by columns, same as for column in booleandf.columns:
for column in booleandf:
pandasDF[column] = pandasDF[column].map(booleanDictionary)

print (pandasDF)
A B C
0 TRUE 4 FALSE
1 FALSE 5 TRUE
2 TRUE 6 FALSE

EDIT:

Simplier solution with replace by dict:

booleanDictionary = {True: 'TRUE', False: 'FALSE'}
pandasDF = pandasDF.replace(booleanDictionary)
print (pandasDF)
A B C
0 TRUE 4 FALSE
1 FALSE 5 TRUE
2 TRUE 6 FALSE


Related Topics



Leave a reply



Submit