How to split a column data into other columns which is stored in a dataframe?
<p>The df is the dataframe which contain the following information.</p>

<pre><code> In [61]: df.head()
    Out[61]: 
       id  movie_id                  info
    0   1         1   Italy:1 January 1994
    1   2         2   USA:22 January 2006
    2   3         3   USA:12 February 2006
    3   4         4   USA:February 2006
    4   5         5   USA:2006
</code></pre>

<p>I want output like below:</p>

<pre><code>In [61]: df.head()    
Out[61]: 
   id  movie_id    country Date    Month   Year
0   1         1    Italy    1     January  1994
1   2         2    USA      22    January  2006
2   3         3    USA      12    February 2006
3   4         4    USA      None  February 2006
4   5         5    USA      None  None     2006
</code></pre>

<p>The data is stored in dataframe and it must be overwrite into the dataframe.</p>

<p>You can use regex <code>:|\s+</code> to split the column on either semicolon or white spaces and specify the <code>expand</code> parameter to be true so that the result will expand to columns:</p>

<pre><code>df[["country","Date","Month","Year"]] = df['info'].str.split(':|\s+', expand = True)
</code></pre>

<p><a href="https://i.stack.imgur.com/34WnK.png" rel="nofollow"><img src="https://i.stack.imgur.com/34WnK.png" alt="enter image description here"></a></p>

<p><em>Update</em>:</p>

<p>To handle optional missing dates and months, you could try <code>extract</code> with regular expression:</p>

<pre><code>(df[["country","Date","Month","Year"]] = 
     df['info'].str.extract('^([A-Za-z]+):(\d{1,2})? ?([A-Za-z]+)? ?(\d{4})$'))
</code></pre>

<ul>
<li><code>^([A-Za-z]+):(\d{1,2})? ?([A-Za-z]+)? ?(\d{4})$'</code> contains four capture groups corresponding to <code>country, Date, Month, Year</code> respectively;</li>
<li><code>^</code> and <code>$</code> denote the start and end of the string;</li>
<li><code>([A-Za-z]+)</code> captures the country which is before <code>:</code> and consists of letters; </li>
<li><code>(\d{1,2})</code> captures Date which consists of one or two digits but optional(with <code>?</code> after the group), i.e, could be missing;</li>
<li><code>([A-Za-z]+)</code> captures Month which consists of letters and it's marked as optional with <code>?</code>;</li>
<li><code>(\d{4})</code> captures the year which consists of four digits;</li>
</ul>

<p><a href="https://i.stack.imgur.com/SQGUN.png" rel="nofollow"><img src="https://i.stack.imgur.com/SQGUN.png" alt="enter image description here"></a></p>

python pandas