Apply a split and take second element of result in Pandas column that sometimes contains None and sometimes does not split into more than 1 component
<p>I am looking at an email sender data column that looks like this:</p>

<pre><code>from_name
----------
Joe Smith, VP at Corp
Alice Brown
None
Helpdesk, McRay's Store
</code></pre>

<p>From this, I'd like to generate a column like this:</p>

<pre><code>title_if_any
-------------
VP at Corp
None
None
McRay's Store
</code></pre>

<p>I've tried the following:</p>

<pre><code>email_data['title_email_sender'] = email_data[email_data.from_name.isnull() == False][email_data.from_name.apply(lambda x: ',' in x)].from_name.apply(lambda x: x.split(',')[1])
</code></pre>

<p>But this generates the following error:</p>

<pre><code>----&gt; 2 email_data['title_email_sender'] = email_data[email_data.from_name.isnull() == False][email_data.from_name.apply(lambda x: ',' in x)].from_name.apply(lambda x: x.split(',')[1])

TypeError: argument of type 'NoneType' is not iterable
</code></pre>

<p>Shouldn't my first selection remove all NoneTypes? How can I achieve what I want and fix the above?</p>

<p>Thanks!</p>

<p>you can do <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.str.split.html" rel="nofollow">str.split()</a>:</p>

<pre><code>In[53]:df['title_if_any']=df.from_name.str.split(',',expand=True)[1]

In[54]:df    
Out[54]: 
    0       VP at Corp
    1             None
    2             None
    3     McRays Store
    Name: 1, dtype: object
</code></pre>

python pandas