DataFrame: Concatenate columns without loosing the other columns

Question:

I have dataset of 5 columns, I’ll call them [‘A’, ‘B’, ‘C’, ‘D’, ‘F’) for here.

I am trying to concatenate A, B and C and still have D and F in the dataframe.

Maybe concatenating isn’t even the thing I am looking for? I just want the strings from A, B and C to be in one columns.

I tried to use newdf[‘ABC’] = newdf[‘A’] + newdf[‘B’] + newdf[‘C’]

I thought it would combine the columns into a new column called ‘ABC’ and leave D and F alone but it overwrites the dataframe to have only one column (ABC)

I’m just startinag of so that might be a basic question but I just didn’t find a helping answer.

Thank you in advance!

Asked By: Claud11

||

Answers:

>>> df
   A  B  C  D  E
0  1  2  3  4  5
>>> df['ABC'] = df['A'] + df['B'] + df['C']
>>> df
   A  B  C  D  E  ABC
0  1  2  3  4  5    6

It does not overwrite D, E columns.

If it’s not the one you want, can you explain your code and input/output?

Answered By: jaemmin
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.