title: Concatenate some columns in Panda Dataframe?
body: <p>Trying to figure out how to do this. I have a text file that has data in it, such as the example below. When I read in the file using read_csv, I am left with columns of strings. I am trying to concatenate columns of each row into one column.</p>
  <p>There will be more data in other columns that I can't concatenate, so my thinking was that with the for loop, I can change it to not loop through those columns.</p>
  <p>.txt file:</p>
  <pre><code>Test data 1 1
  Test data 2 2 2
  Test data 3 3 3 3
  Test data
  </code></pre>
  <p>My previous method was to find the lastcol and use that as the maximum, then use a for loop.</p>
  <pre><code>df = pd.read_csv(filename, sep='\s+', header=None)
  cols = list(df.columns)
  lastcol = cols[-1]
  n = 0
  df['testColumn'] = df[n].map(str)
  for n in range (0,lastcol):
     df = df.replace(np.nan, '', regex=True)
     df['testColumn'] = df['testColumn'] + ' ' + df[n+1].map(str)
  </code></pre>
  <p>This works fine when the data in row 1 has the most columns. However, this fails upon entering the second row if the data row 2 has more columns than the row 1, such as in the .txt file example.</p>
  <p>I am thinking my issue now is in how I am grabbing the end column. I am somewhat new to Python.</p>
  <p>Here is the current output of when a text file has the first row with the most columns:</p>
  <pre><code>      0     1  2  3  4            testColumn
  0  Test  data  1  1  1  Test data 1 1.0 1.0
  1  Test  data  2  2        Test data 2 2.0 
  2  Test  data  3              Test data 3  
  </code></pre>
  <p>Here is the current output error when a text file has the most columns in a different row (using text file example above):</p>
  <pre><code>pandas.errors.ParserError: Error tokenizing data. C error: Expected 4 fields in line 2, saw 5
  </code></pre>