Reputation: 40
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 | | 1
How do I fix this?
Upvotes: 1
Views: 120
Reputation: 61910
You could do
df['combined'] = df.fillna(' ').ne(' ').any(axis=1).astype(int)
print(df)
Output
names ages combined
0 John 8 1
1 Jill 1
2 Michael 1
Upvotes: 1