Pandas: Create another column while splitting each row from the first column
<p>Goal create a second column from the first column</p>

<pre><code>column1, column2
Hello World, #HelloWord
US Election, #USElection
</code></pre>

<p>I have a simple file that has a one column</p>

<pre><code>columnOne
Hello World
US Election
Movie Night
</code></pre>

<p>I wrote following function</p>

<pre><code>&gt;&gt;&gt; def newColumn(row):
...     r = "#" + "".join(row.split(" "))
...     return r
</code></pre>

<p>then I did following to create the second column using pandas</p>

<pre><code>df['column2'] = df.apply (lambda row: newColumn(row),axis=1)
</code></pre>

<p>But I end up with following error: </p>

<pre><code>Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "/Users/anuradha_uduwage/anaconda2/lib/python2.7/site-packages/pandas/core/frame.py", line 3972, in apply
    return self._apply_standard(f, axis, reduce=reduce)
  File "/Users/anuradha_uduwage/anaconda2/lib/python2.7/site-packages/pandas/core/frame.py", line 4064, in _apply_standard
    results[i] = func(v)
  File "&lt;stdin&gt;", line 1, in &lt;lambda&gt;
  File "&lt;stdin&gt;", line 2, in newColumn
  File "/Users/anuradha_uduwage/anaconda2/lib/python2.7/site-packages/pandas/core/generic.py", line 2360, in __getattr__
    (type(self).__name__, name))
AttributeError: ("'Series' object has no attribute 'split'", u'occurred at index 0')
</code></pre>

<p>so I change the split to following: </p>

<pre><code>r = "".join(row.str.split(" "))
</code></pre>

<p>But that didn't help</p>

<p>This should do the trick</p>

<pre><code>df['new_column'] = df['old_column'].apply(lambda x: "#"+x.replace(' ', ''))
</code></pre>

<p>Example</p>

<pre><code>&gt;&gt;&gt; names = ['Hello World', 'US Election', 'Movie Night']
&gt;&gt;&gt; df = pd.DataFrame(data = names, columns=['Names'])
&gt;&gt;&gt; df
     Names
0    Hello World
1    US Election
2    Movie Night

&gt;&gt;&gt; df['Names2'] = df['Names'].apply(lambda x: "#"+x.replace(' ', ''))
&gt;&gt;&gt; df
     Names         Names2
0    Hello World   #HelloWorld
1    US Election   #USElection
2    Movie Night   #MovieNight
</code></pre>

python pandas