Pandas – Convert 2 string columns to booleans and combine

  merge, pandas, python

you guys! I’m new to Pandas, and I’m kind of stumped on this issue here. I have 2 columns with text in them. I want to create a new column that is a binary representation of boolean values. The 2 columns should be converted to true if they have a value or false if they don’t have a value. I’ve tried using notnull() and isnull(), but the empty values are resulting to True instead of False.

I want to take this:

    dicts = {"names": ["John", "Jill", "Michael"], "ages": ["8", " ", " "]}
    df = pd.DataFrame(dicts)

and turn it into this (where the null values evaluate to false:

---------------------
|  names   |  ages  |
----------------------
   True    |   True
   True    |   False
   True    |   False

Then merge them (where the True values are dominant):

--------------------------------
|  names   |  ages  |  combined |
---------------------------------
   True    |  True  |   True
   True    |  False |   True
   True    |  False |   True

The final product should look like this:

--------------------------------
|  names   |  ages  |  combined |
---------------------------------
   John    |    8   |     1
   Jill    |        |     1
  Michael  |        |     0

 

How do I fix this?

Source: Python Questions

LEAVE A COMMENT