Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9083117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:42:26+00:00 2026-06-16T20:42:26+00:00

I have a dataframe filled with True and False values, and I’d like to

  • 0

I have a dataframe filled with True and False values, and I’d like to get a dataframe from it with the True replaced with 1 and the False replaced with np.NaN. I’ve tried using dataframe.replace, but it gave a dataframe filled with all True. Is there a way to do it without using for loops and if’s?

Example, this is the dataframe I have, with T for True and F for False (not strings ‘T’ and ‘F’; sorry, could not figure out how to format a nicely spaced table in the wiki):

2008-01-02 16:00:00 T T F
2008-01-03 16:00:00 T T T
2008-01-04 16:00:00 T T F
2008-01-07 16:00:00 T T T
2008-01-08 16:00:00 T T F

This is what I would like to change it to:

2008-01-02 16:00:00 1 1 np.NaN
2008-01-03 16:00:00 1 1 1
2008-01-04 16:00:00 1 1 np.NaN
2008-01-07 16:00:00 1 1 1
2008-01-08 16:00:00 1 1 np.NaN

These are the lines I tried to replace the True and False, and got a dataframe filled with all True values:

df.replace(to_replace=True, value=1, inplace=True, method=None)   
df.replace(to_replace=False, value=np.NAN, inplace=True, method=None)  

When tried separately, the first line alone does not change anything; the second line converts all the values to True.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T20:42:27+00:00Added an answer on June 16, 2026 at 8:42 pm

    applymap() can be used to apply a function to every element of a dataframe

    In [1]: df = DataFrame([[True, True, False],[False, False, True]]).T
    
    In [2]: df
    Out[2]:
           0      1
    0   True  False
    1   True  False
    2  False   True
    
    In [3]: df.applymap(lambda x: 1 if x else np.nan)
    Out[3]:
        0   1
    0   1 NaN
    1   1 NaN
    2 NaN   1
    

    You can also use a dict:

    In [4]: d = {True:1, False:np.nan}
    
    In [5]: df.applymap(lambda x: d[x])
    Out[5]:
        0   1
    0   1 NaN
    1   1 NaN
    2 NaN   1
    

    Addressing DSM’s comment from below. I misread the OP and assumed the datetime was an index. If it’s not an index this worked for me:

    In [6]: df.applymap(lambda x: d.get(x,x))
    Out[6]:
        0   1                    2
    0   1 NaN  2012-01-01 00:00:00
    1 NaN   1  2012-01-01 00:00:00
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dataframe where some of the values are NA. I would like
I have a dataframe of values and I would like to explore the rows
I have a dataframe x with this values: x1 x2 x3 1 NA 4
I have a dataframe with numeric entries like this one test <- data.frame(x =
I have a dataframe with a column of integers that I would like to
I have a dataframe and would like to add a new column where the
I have a dataframe filled with course IDs, student IDs, week numbers (1 for
I have two dataframes and I wish to insert the values of one dataframe
I have a dataframe that looks like this: step var1 score1 score2 1 a
I have a dataframe like so: item price source day book 5 shop 2012-01-01

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.