In pandas, set a new column and update existing column
<p>In a <code>pandas</code> dataframe, I have a last name field that looks like</p>

<pre><code>df = pd.DataFrame(['Jones Jr', 'Smith'], columns=['LastName'])
</code></pre>

<p>I am trying to set a new column named 'Generation', while stripping out the Generation for the last name, so the outcome would look like this:</p>

<pre><code>df2 = pd.DataFrame([('Jones', 'Jr'), ('Smith', '')], columns=['LastName', 'Generation'])
</code></pre>

<p>I could set the Generation column then go back and remove the Generation from the last name:</p>

<pre><code>df.loc[df['LastName'].str[-3:] == ' Jr', 'Generation'] = 'Jr'
df.loc[df['LastName'].str[-3:] == ' Jr', 'LastName'] = df['LastName'].str[:-3]
</code></pre>

<p>However, that's two steps and it seems like performing an update in one swoop would be best. </p>

<p>I thought about apply, but it's an apply to two columns where matching X and I couldn't find anything close to that.  </p>

<p>You can use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.str.extract.html" rel="nofollow">.str.extract()</a> method:</p>

<pre><code>In [19]: df2 = df.LastName.str.extract(r'(?P&lt;LastName&gt;\w+)\s?(?P&lt;Generation&gt;Jr|Sr)?', expand=True)

In [20]: df2
Out[20]:
  LastName Generation
0    Jones         Jr
1    Smith        NaN
</code></pre>

python pandas dataframe