title: Combining columns in Pandas based on column header
body: <p>I'm needing to merge columns in a dataframe.</p>
  
  <p>The headers will have a similar name with a different suffix, e.g.  </p>
  
  <pre><code>A1 | A2 | A3 | B1 | B2 | B3
  </code></pre>
  
  <p>I want to end up with all of them merged:</p>
  
  <pre><code>A | B
  </code></pre>
  
  <p>I have this line that successfully merges a defined set of columns into a single column:</p>
  
  <pre><code>df['A'] = df[['A1','A2','A3]].apply(' '.join, axis=1)
  </code></pre>
  
  <p>The problem is that the headers are inconsistent in that there might be any combination of '1','2',or '3' - e.g.</p>
  
  <pre><code>A1 | A2 | A3 | B2 | C1 | C2 
  </code></pre>
  
  <p>From the solutions I've looked at, pandas doesn't like to reference columns that don't exist, so I can't use <code>apply</code> statement as a blanket command. </p>
  
  <p>I'm having trouble visualizing a solution beyond a list of nested Try/Except steps. If anyone has an idea, I would appreciate it!</p>
  
  <p><strong>Update</strong><br>
  Thanks for the solutions!!! If anyone is interested, here's what worked for me:  </p>
  
  <p><em>Solution 1</em></p>
  
  <pre><code>for h in headers:
      cols = [col for col in df.columns if col.split('[')[0] == h]
      if cols == []:
          cols = [col for col in df.columns if col == h and col.split('[')[0] not in headers] `
  </code></pre>
  
  <p><em>Solution 2</em></p>
  
  <pre><code>df.groupby(df.columns.str.split('[').str[0],axis=1).agg(lambda x :' '.join(x.values.tolist()))
  </code></pre>