Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

concatenate two columns values pandas

I have a dataframe data :

Cluster OsId    BrowserId   PageId  VolumePred  ConversionPred  
255      7         11          17   1149582        4.0  
607     18         99          16   917224         8.0  
22       0         12          14   1073848        4.0  

I would like to add new column "OSBROWSER" which is the concatenation of two columns : OsId and BrowserId.

The result should be like this :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Cluster OsId    BrowserId   PageId  VolumePred  ConversionPred  OSBROWSER
255      7         11          17   1149582        4.0           (7, 11)
607     18         99          16   917224         8.0           (18, 99)
22       0         12          14   1073848        4.0           (0, 12)

I try like this :

data['OSBrowser'] =  data["OsId"] + data["BrowserId"]

But it gave me the sum of the two clumns values

Any idea please? thanks you

SOLUTION :

data['OSBrowser']  = list(zip(data.OsId, data.BrowserId))

>Solution :

I would convert the columns to string, I think that’s what you’re looking to do.

df = pd.DataFrame(((123, 456, 789), (98, 765, 432)), columns=('a', 'b', 'c'))

df['a_str'] = df['a'].astype(str)
df['b_str'] = df['b'].astype(str)

df['ab'] = df['a_str'] + df['b_str']

df then looks like this

     a    b    c a_str b_str      ab
0  123  456  789   123   456  123456
1   98  765  432    98   765   98765

Then you can just drop a_str and b_str

df = df[['a', 'b', 'c', 'ab']]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading