I have multiple columns that I want to merge together, but I want it driven by a table. So a user can specify let's say fields: 'A', 'B', 'C' and another use can say they want 'A', 'B', 'E', 'Z', etc...
So I want this to be dynamic in a sense. In addition between each row value, I need to insert a \n value to have a new line character.
So in my field description, given the first example, I need the string to be formatted as:
'A: ' + df['A'] + "\n" + 'B: ' + df['B'] + '\n' + 'C: ' + df['C'] + '\n'
When working with string data in python, you can leverage the += operation to append text to an existing string, so I thought I could create a column called description on my Pandas dataframe object and then use the += operation to push everything together.
df['description'] = ""
for f in fields:
df['description'] += f + ": " + df[f] + '\n'
But this raises a typeerror:
TypeError: unsupported operand type(s) for +: 'numpy.ndarray' and 'str'
Any ideas on how I can do this?
I am using Pandas 0.16.1
Thanks
df['description'] = df['description'] + ...work? If so, that's what you should use.fieldsvariable in the linefor f in fields:?pandas 0.19.1