Benoit Demeule
Benoit Demeule

Reputation: 9

Concatenate value from 3 excel columns in Python

very new to Python here.

I'm triying to concatenate value from 3 columns from an excel sheet into 1 columns. I do have about 300-400 rows to do

Values are like this

COl1   COL 2   COL3
CNMG   432     EMU
TNMG   332     ESU
...

Output should be

COL3
CNMG432EMU
TNMG332ESU
...

I tried about every tutorial in Pandas I could find, but nothing seams to works since I have STR and INT

Thanks in advance

Upvotes: 0

Views: 190

Answers (3)

PydPiper
PydPiper

Reputation: 498

You can also do this pretty easily in pylightxl https://pylightxl.readthedocs.io/en/latest/

import pylightxl as xl

db = xl.readxl(‘excelfile.xlsx’)

cat_3_columns = list(zip(db.ws(‘Sheet1’).col(1), db.ws(‘Sheet1’).col(2), db.ws(‘Sheet1’).col(3)))

Upvotes: 0

Gingerhaze
Gingerhaze

Reputation: 706

df["COL3"]=df["COL1"]+df["COL2"].astype(str)+df["COL3]

Upvotes: 0

Umar.H
Umar.H

Reputation: 23099

seems like some simple string concatenation should do the trick

df['concat'] = df['COL1'] + df['COL 2'].astype(str) + df['COL3']

if you have ints, you'll need to cast them as strings, you can check which columns with a simple print(df.dtypes)

if you have ints or floats you'll need to use .astype(str)

print(df)
   COl1  COL2 COL3      concat
0  CNMG   432  EMU  CNMG432EMU
1  TNMG   332  ESU  TNMG332ESU

Upvotes: 2

Related Questions