-2

I get a data extract which imports into pandas as a block of valid columns to the left and a block of Unnamed columns to the right. I need to concatenate the contents of the Unnamed columns into a string for each row. Each column needs to be separated by a space in the resulting string. In short how do I return all "Unnamed:*" columns, iterate over the corresponding rows and concatenate all the contents, separated by spaces? There are empty cells also. I use keep_default_na=False when importing to keep the empty cells as I need them.

  • Please provide sample data in a [reproducible way](https://stackoverflow.com/questions/20109391). Otherwise people won't be able to test. – Bill Huang Nov 28 '20 at 19:30
  • Please structure your questions using paragraphs and use ``` ``` for annotating code. – Marc Nov 28 '20 at 19:32
  • Welcome to Stack Overflow. Please edit the question to include a [*"Minimal, Reproducible, Example."*](https://stackoverflow.com/help/minimal-reproducible-example) – bad_coder Nov 29 '20 at 00:35

1 Answers1

1

You can combine them as:

df.loc[:,df.columns.str.startswith('Unnamed:')].apply(lambda x:' '.join(x.values.astype(str)), axis = 1)

If you want them to be in one column:

df['Combined'] = df.loc[:,df.columns.str.startswith('Unnamed:')].apply(lambda x:' '.join(x.values.astype(str)), axis = 1)
Hamza
  • 4,186
  • 2
  • 21
  • 39