Python pandas conditional column dealing with None
<p>If I have pandas data like this:</p>

<pre><code>s1    s2    s3
 1    None   1
 1    2      1
 2    2      2
 1    2     None
</code></pre>

<p>I want to add a new column 's' whose value will be None if the values of s1, s2 and s3 don't match. If they match (I want to ignore None in this comparision) the value should be the common value. So the output will be </p>

<pre><code>s1    s2    s3     s
 1    None   1     1        (Ignoring None in comparision here)
 1    2      1    None
 2    2      2     2
 1    2     None  None
</code></pre>

<p>What is the best way to introduce this new conditional column in pandas?</p>

<p>Assuming your columns are numeric and None's are treated as NaN's, you can do:</p>

<pre><code>df['s'] = np.where(df.std(axis=1)==0, df.mean(axis=1), np.nan)

df
Out: 
   s1   s2   s3    s
0   1  NaN  1.0  1.0
1   1  2.0  1.0  NaN
2   2  2.0  2.0  2.0
3   1  2.0  NaN  NaN
</code></pre>

<p>This is based on the fact that if all values are equal, the standard deviation of that row will be 0, and the mean will be equal to the those numbers. Both mean and the standard deviation calculations ignore NaNs. </p>

<p>If the first assumption is not correct, please replace None's first:</p>

<pre><code>df = df.replace({'None': np.nan})
</code></pre>

<p>where np is numpy (<code>import numpy as np</code>).</p>

python pandas