0

I have a dataset that looks like that:

          true_time       amount     name      value  fruit
2019-11-28 12:57:00        0.59       AAA      81.98  apple
2019-11-28 12:58:00        2.37       BBB     261.98  orange
2019-11-28 12:59:00       559.4       CCC      71.45  banana

I'm trying to add an extra column to the end called concat which should contain a string of joined values from certain cells on the same row.

The result looks like this:

... fruit     concat
    apple     0.59-AAA-81.98
    orange    2.37-BBB-261.98
    banana    559.4-CCC-71.45

I tried bits and pieces but was not sure how to access multiple cells per row and ended up with this incomplete attempt.

df['concat'] = df['concat'].apply(lambda x: '{}-{}-{}'.format(loc[0], loc[1], loc[2]))

I might be needing a standalone function as I'm planning to hash the string and add the result to a hash column next to the concat column.

mbilyanov
  • 2,315
  • 4
  • 29
  • 49
  • This is a duplicate. I apologise, I have missed this exact same question and the answer. https://stackoverflow.com/questions/39291499/how-to-concatenate-multiple-column-values-into-a-single-column-in-panda-datafram – mbilyanov Dec 08 '19 at 21:07

1 Answers1

1

Just add them like you would normally do with strings. Here's an easily reproducible example:

import pandas as pd

df = pd.DataFrame({'Name':['Mark', 'Laura', 'Adam', 'Roger', 'Anna'],
                   'Car':['Audi', 'Tesla', 'BMW', 'Ford', 'Hyundai'],
                   'Money':[0, 50, 20, 15, 10]})

print(df)
Out[31]: 
    Name      Car  Money
0   Mark     Audi      0
1  Laura    Tesla     50
2   Adam      BMW     20
3  Roger     Ford     15
4   Anna  Hyundai     10
df['concat'] = df['Name'] + '-' + df['Money'].astype(str) + '-' + df['Car']
print(df)
Out[35]: 
    Name      Car  Money           concat
0   Mark     Audi      0      Mark-0-Audi
1  Laura    Tesla     50   Laura-50-Tesla
2   Adam      BMW     20      Adam-20-BMW
3  Roger     Ford     15    Roger-15-Ford
4   Anna  Hyundai     10  Anna-10-Hyundai

For numeric columns, don't forget to add df['col'].astype(str)

Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143