-1

I have the next doubt. How does that "for iteration" inside the join works?

df = pd.DataFrame({'Col1': ['2014', '2015'],
    'Col2': ['40000', '50000']})
df['cost'] = df[['Col1', 'Col2']].apply(
    lambda x: '|'.join([str(i) for i in x]), axis=1)+'|'
RichieV
  • 5,103
  • 2
  • 11
  • 24
Christian
  • 29
  • 6
  • google "python list comprehensions", here is one explanation https://stackoverflow.com/q/47789/6692898 – RichieV Sep 01 '20 at 15:18

1 Answers1

0

It basically iterates x and cast each element of x into a string. The result is a list with string representations of the elements of x.

Given e.g. a string "test", the result would be ['t', 'e', 's', 't'] and after the join operation you would end up with 't|e|s|t'

In your case, you would end up with 2014|40000| and 2015|50000|

Note that the last | is due to the +'|'

Manuel
  • 546
  • 1
  • 5
  • 17