Joining string of a columns over several index while keeping other colums
<p>Here is an example data set:</p>

<pre><code>&gt;&gt;&gt; df1 = pandas.DataFrame({
    "Name": ["Alice", "Marie", "Smith", "Mallory", "Bob", "Doe"],
    "City": ["Seattle", None, None, "Portland", None, None],
    "Age": [24, None, None, 26, None, None],
    "Group": [1, 1, 1, 2, 2, 2]})

&gt;&gt;&gt; df1
    Age      City  Group     Name
0  24.0   Seattle      1    Alice
1   NaN      None      1    Marie
2   NaN      None      1    Smith
3  26.0  Portland      2  Mallory
4   NaN      None      2      Bob
5   NaN      None      2      Doe
</code></pre>

<p>I would like to merge the Name column for all index of the same group while keeping the City and the Age wanting someting like:</p>

<pre><code>&gt;&gt;&gt; df1_summarised
    Age      City  Group     Name
0  24.0   Seattle      1    Alice Marie Smith
1  26.0  Portland      2    Mallory Bob Doe
</code></pre>

<p>I know those 2 columns (Age, City) will be NaN/None after the first index of a given group from the structure of my starting data. </p>

<p>I have tried the following:</p>

<pre><code>&gt;&gt;&gt; print(df1.groupby('Group')['Name'].apply(' '.join))
Group
1    Alice Marie Smith
2      Mallory Bob Doe
Name: Name, dtype: object
</code></pre>

<p>But I would like to keep the Age and City columns...</p>

<p>using <code>dropna</code> and <code>assign</code> with <code>groupby</code></p>

<p><a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.assign.html" rel="nofollow">docs to assign</a></p>

<pre><code>df1.dropna(subset=['Age', 'City']) \
   .assign(Name=df1.groupby('Group').Name.apply(' '.join).values)
</code></pre>

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

<hr>

<p><strong><em>timing</em></strong><br>
per request</p>

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

<hr>

<p><strong><em>update</em></strong><br>
use <code>groupby</code> and <code>agg</code><br>
I thought of this and it feels far more satisfying</p>

<pre><code>df1.groupby('Group').agg(dict(Age='first', City='first', Name=' '.join))
</code></pre>

<p>to get the exact output</p>

<pre><code>df1.groupby('Group').agg(dict(Age='first', City='first', Name=' '.join)) \
   .reset_index().reindex_axis(df1.columns, 1)
</code></pre>

python pandas