Iteratively concatenate columns in pandas with NaN values
<p>I have a <code>pandas.DataFrame</code> data frame:</p>

<pre><code>import pandas as pd

df = pd.DataFrame({"x": ["hello there you can go home now", "why should she care", "please sort me appropriately"], 
    "y": [np.nan, "finally we were able to go home", "but what about meeeeeeeeeee"],
    "z": ["", "alright we are going home now", "ok fine shut up already"]})

cols = ["x", "y", "z"]
</code></pre>

<p>I want to iteratively concatenate these columns, as opposed to writing something like:</p>

<pre><code>df["concat"] = df["x"].str.cat(df["y"], sep = " ").str.cat(df["z"], sep = " ")
</code></pre>

<p>I know that three columns seems trivial to put together, but I actually have 30. so, I would like to do something like:</p>

<pre><code>df["concat"] = df[cols[0]]
for i in range(1, len(cols)):
    df["concat"] = df["concat"].str.cat(df[cols[i]], sep = " ")
</code></pre>

<p>Right now, the initial <code>df["concat"] = df[cols[0]]</code> line works fine, but the <code>NaN</code> value in location <code>df.loc[1, "y"]</code> messes up the concatenation. Ultimately, the entire <code>1</code>st row ends up as <code>NaN</code> in <code>df["concat"]</code> due to this one null value. How can I get around this? Is there some option with <code>pd.Series.str.cat</code> I need to specify?</p>

<p><strong><em>Option 1</em></strong></p>

<pre><code>pd.Series(df.fillna('').values.tolist()).str.join(' ')

0                    hello there you can go home now  
1    why should she care finally we were able to go...
2    please sort me appropriately but what about me...
dtype: object
</code></pre>

<p><strong><em>Option 2</em></strong></p>

<pre><code>df.fillna('').add(' ').sum(1).str.strip()

0                      hello there you can go home now
1    why should she care finally we were able to go...
2    please sort me appropriately but what about me...
dtype: object
</code></pre>

python pandas