How to row-wise concatenate several columns containing strings?
<p>I have a specific series of datasets which come in the following general form:</p>

<pre><code>import pandas as pd
import random
df = pd.DataFrame({'n': random.sample(xrange(1000), 3), 't0':['a', 'b', 'c'], 't1':['d','e','f'], 't2':['g','h','i'], 't3':['i','j', 'k']})
</code></pre>

<p>The number of <em>tn</em> columns (<em>t0, t1, t2 ... tn</em>) <strong>varies depending on the dataset</strong>, but is always &lt;30.
My aim is to merge content of the <em>tn</em> columns for each row so that I achieve this result (note that for readability I need to keep the whitespace between elements):</p>

<pre><code>df['result'] = df.t0 +' '+df.t1+' '+df.t2+' '+ df.t3
</code></pre>

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

<p>So far so good. This code may be simple but it becomes clumsy and inflexible as soon as I receive another dataset, where the number of <em>tn</em> columns goes up. This where my question comes in:</p>

<p><strong>Is there any other syntax to merge the content across multiple columns?</strong> Something agnostic to the number columns, akin to:</p>

<pre><code>df['result'] = ' '.join(df.ix[:,1:])
</code></pre>

<p>Basically I want to achieve the same as the OP in the link below, but with whitespace between the strings:
<a href="http://stackoverflow.com/questions/6308933/r-concatenate-row-wise-across-specific-columns-of-dataframe">R - concatenate row-wise across specific columns of dataframe</a></p>

<p>Here is a slightly alternative solution:</p>

<pre><code>In [57]: df['result'] = df.filter(regex=r'^t').apply(lambda x: x.add(' ')).sum(axis=1).str.strip()

In [58]: df
Out[58]:
     n t0 t1 t2 t3   result
0   92  a  d  g  i  a d g i
1  916  b  e  h  j  b e h j
2  363  c  f  i  k  c f i k
</code></pre>

python pandas dataframe multiple-columns string-concatenation