0

I have:

df1 = [['10103', 'Baldwin, C', 'SFEN'], ['10115', 'Wyatt, X', 'SFEN']]

What would be the best way to concatenate the items within so that I could get something like:

[['10103 - Baldwin, C', 'SFEN'], ['10115 - Wyatt, X', 'SFEN']]

If I try something like

for i in df1:
    df1[0:1] = [' - '.join(df1[0:1])]

I get TypeError: sequence item 0: expected str instance, list found.

1
  • 1
    join take list of string, you are passing list of list of string Commented Nov 14, 2020 at 3:03

1 Answer 1

1

Do the same way as yours,

>>> df1 = [['10103', 'Baldwin, C', 'SFEN'], ['10115', 'Wyatt, X', 'SFEN']]
>>> for i, item in enumerate(df1):
...     df1[i] = [' - '.join(item[:2]), item[2]]
...
>>> df1
[['10103 - Baldwin, C', 'SFEN'], ['10115 - Wyatt, X', 'SFEN']]

Another way to go,

>>> df1 = [['10103', 'Baldwin, C', 'SFEN'], ['10115', 'Wyatt, X', 'SFEN']]
>>> df1 = [[' - '.join([item1, item2]), item3] for item1, item2, item3 in df1]
>>> df1
[['10103 - Baldwin, C', 'SFEN'], ['10115 - Wyatt, X', 'SFEN']]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.