OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Boolean indexing: How to select a specific value to create new column

  • Thread starter Thread starter iBeMeltin
  • Start date Start date
I

iBeMeltin

Guest
I have a dataframe which looks like this:

Code:
col1 col2 col3
X1   Nan  Nan
foo  bar  baz
foo  bar  baz
X2   Nan  Nan
foo  bar  baz
foo  bar  baz
X3   Nan  Nan
foo  bar  baz
foo  bar  baz

I have filtered to look like this:

Code:
m = df.notna()
print(m)

Code:
col1 col2 col3
True False False
True True True
True True True
True False False 
True True True
True True True 
True False False 
True True True 
True True True

I need to select the True value in the row with False's, and create a new column with that value. For example, my resultant df should essentially look like this:

Code:
col1 col2 col3 new
foo  bar  baz  X1
foo  bar  baz  X1
foo  bar  baz  X2
foo  bar  baz  X2
foo  bar  baz  X3
foo  bar  baz  X3

I am unsure how to accomplish this with pandas, any suggestions would help
<p>I have a dataframe which looks like this:</p>
<pre><code>col1 col2 col3
X1 Nan Nan
foo bar baz
foo bar baz
X2 Nan Nan
foo bar baz
foo bar baz
X3 Nan Nan
foo bar baz
foo bar baz

</code></pre>
<p>I have filtered to look like this:</p>
<pre><code>m = df.notna()
print(m)
</code></pre>
<pre><code>col1 col2 col3
True False False
True True True
True True True
True False False
True True True
True True True
True False False
True True True
True True True
</code></pre>
<p>I need to select the True value in the row with False's, and create a new column with that value.
For example, my resultant df should essentially look like this:</p>
<pre><code>col1 col2 col3 new
foo bar baz X1
foo bar baz X1
foo bar baz X2
foo bar baz X2
foo bar baz X3
foo bar baz X3
</code></pre>
<p>I am unsure how to accomplish this with pandas, any suggestions would help</p>
 

Latest posts

I
Replies
0
Views
1
impact christian
I
Top