Shannon Sweet
Shannon Sweet

Reputation: 11

Merging two columns with different information, python

I have a dataframe with one column of last names, and one column of first names. How do I merge these columns so that I have one column with first and last names?

Here is what I have: First Name (Column 1)

John
Lisa
Jim

Last Name (Column 2)

Smith

Brown

Dandy

This is what I want:

Full Name

John Smith

Lisa Brown

Jim Dandy.

Thank you!

Upvotes: 0

Views: 3623

Answers (2)

Vaishali
Vaishali

Reputation: 38415

Try

df.assign(name = df.apply(' '.join, axis = 1)).drop(['first name', 'last name'], axis = 1)

You get

    name
0   bob smith
1   john smith
2   bill smith

Upvotes: 5

Andrew L
Andrew L

Reputation: 7038

Here's a sample df:

df
  first name last name
0        bob     smith
1       john     smith
2       bill     smith

You can do the following to combine columns:

df['combined']= df['first name'] + ' ' + df['last name']

df
  first name last name    combined
0        bob     smith   bob smith
1       john     smith  john smith
2       bill     smith  bill smith

Upvotes: 4

Related Questions