Splitting strings inside a pandas dataframe
<p>I have a dataframe with one row that has a list like structure</p>

<pre><code>import pandas as pd

df=pd.DataFrame({'Name':['Stooge, Nick','Dick, Tracy','Rick, Nike','Maw','El','Paw, Maw, Haw','Caw', 'Greep'],
'key':[2,2,2,1,1,3,1,1,],
'Lastname':['Smith, Foo','Johnson, Macy','Johnson, Sike','Simpson','Diablo','Simpson, Sampson, Simmons','Simpson', 'Mortimer']
})


df.ix[df['key'] == 2, 'Full'] =  df['Name']+', ' + df['Lastname']
df.ix[df['key'] == 1, 'Full'] = df['Name']+' ' + df['Lastname']
print(df)
</code></pre>

<p>Output:</p>

<pre><code>                    Lastname           Name  key                        Full
0                 Smith, Foo   Stooge, Nick    2    Stooge, Nick, Smith, Foo
1              Johnson, Macy    Dick, Tracy    2  Dick, Tracy, Johnson, Macy
2              Johnson, Sike     Rick, Nike    2   Rick, Nike, Johnson, Sike
3                    Simpson            Maw    1                 Maw Simpson
4                     Diablo             El    1                   El Diablo
5  Simpson, Sampson, Simmons  Paw, Maw, Haw    3                         NaN
6                    Simpson            Caw    1                 Caw Simpson
7                   Mortimer          Greep    1              Greep Mortimer
</code></pre>

<p>Is there a way manipulate or split the string inside the dataframe by the comma so it produces results like:</p>

<pre><code>                    Lastname           Name  key                        Full
0                 Smith, Foo   Stooge, Nick    2    Stooge Smith and Nick Foo
1              Johnson, Macy    Dick, Tracy    2  Dick Johnson and Tracy Macy
2              Johnson, Sike     Rick, Nike    2   Rick Johnson and Nike Sike
3                    Simpson            Maw    1                 Maw Simpson
4                     Diablo             El    1                   El Diablo
5  Simpson, Sampson, Simmons  Paw, Maw, Haw    3                         NaN
6                    Simpson            Caw    1                 Caw Simpson
7                   Mortimer          Greep    1              Greep Mortimer
</code></pre>

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

<pre><code>In [63]: df
Out[63]: 
                    Lastname           Name  key                        Full
0                 Smith, Foo   Stooge, Nick    2    Stooge, Nick, Smith, Foo
1              Johnson, Macy    Dick, Tracy    2  Dick, Tracy, Johnson, Macy
2              Johnson, Sike     Rick, Nike    2   Rick, Nike, Johnson, Sike
3                    Simpson            Maw    1                 Maw Simpson
4                     Diablo             El    1                   El Diablo
5  Simpson, Sampson, Simmons  Paw, Maw, Haw    3                         NaN
6                    Simpson            Caw    1                 Caw Simpson
7                   Mortimer          Greep    1              Greep Mortimer

In [64]: def get_full_name(row):
    ...:     if ',' in str(row.Full):
    ...:        z = row.Full.split(',')
    ...:        x = z[::2]
    ...:        y = z[1::2]
    ...:        return ' and '.join(map(lambda(first, last): ' '.join([first, last]), zip(z, y)))
    ...:     return row.Full
    ...: 

In [65]: df['Full'] = df.apply(get_full_name, axis = 1)

In [66]: df
Out[66]: 
                    Lastname           Name  key                          Full
0                 Smith, Foo   Stooge, Nick    2   Stooge  Nick and  Nick  Foo
1              Johnson, Macy    Dick, Tracy    2  Dick  Tracy and  Tracy  Macy
2              Johnson, Sike     Rick, Nike    2    Rick  Nike and  Nike  Sike
3                    Simpson            Maw    1                   Maw Simpson
4                     Diablo             El    1                     El Diablo
5  Simpson, Sampson, Simmons  Paw, Maw, Haw    3                           NaN
6                    Simpson            Caw    1                   Caw Simpson
7                   Mortimer          Greep    1                Greep Mortimer
</code></pre>

python pandas