python - How do I join the values of two DataFrame columns as one -


i have dataframe data these columns col1 , col1.1.

data: id     col1    col1.1 1      21      water, salt 13     18      onions 101    30      replaceable oil, water, acid 

i want data be:

data: id     col1 1      21: water, salt  13     18: onions 101    30: replaceable oil, water, acid 

so far, have:

data['col'] = ': '.join(str(list(zip(data['col1'], data['col1.1']))).split("', ")).replace("'", "").replace("(", "").replace(")", "").replace("[", "").replace("]", "") 

note: settingwithcopywarning on running this.

how go this? thank all

use str.cat:

df['col1'].astype(str).str.cat(df['col1.1'], sep=': ') out:  0                          21: water, salt 1                               18: onions 2    30: replaceable oil, water, acid 

you need assign , drop other column same output:

df['col1'] = df['col1'].astype(str).str.cat(df['col1.1'], sep=': ') df = df.drop('col1.1', axis=1) 

or, in 1 line, maxu:

df['col1'] = df['col1'].astype(str).str.cat(df.pop('col1.1'), sep=': ') 

Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -