Combine multiple column values into a single column
Lets create a DataFrame with two columns First_Name and Last_Name.
df = pd.DataFrame()
df['First_Name'] = ['John', 'Doe', 'Bill']
df['Last_Name'] = ['Marwel', 'Williams', 'Harry']
df
First_Name Last_Name
0 John Marwel
1 Doe Williams
2 Bill Harry
If both columns (First_Name and Last_Name ) are strings, you can concatenate them directly to a new column.
Join the columns First_Name and Last_Name to a new column FullName
df['FullName'] = df['First_Name'] + df['Last_Name']
df
First_Name Last_Name FullName
0 John Marwel John Marwel
1 Doe Williams Doe Williams
2 Bill Harry Bill Harry
Join Different columns type in Pandas
If one (or both) of the columns are not same typed, you should convert it (them) first and then concatenate them directly to a new column.
df = pd.DataFrame()
df['Name'] = ['John', 'Doe', 'Bill']
df['Age'] = [12, 12, 13]
df
Name Age
0 John 12
1 Doe 12
2 Bill 13
Here Name and Age are different data types, then you have to convert the column types as same and then concatenate it.
df['Name_age'] = df['Name'] + "_" + df['Age'].astype(str)
df
Name Age Name_age
0 John 12 John_12
1 Doe 12 Doe_12
2 Bill 13 Bill_13
Using agg() to join pandas column
If you need to join multiple string columns , you can use agg().
df = pd.DataFrame()
df['First_Name'] = ['John', 'Doe', 'Bill']
df['Last_Name'] = ['Marwel', 'Williams', 'Harry']
df
First_Name Last_Name
0 John Marwel
1 Doe Williams
2 Bill Harry
Using agg()
df['FullName'] = df[['First_Name', 'Last_Name']].agg('-'.join, axis=1)
df
First_Name Last_Name FullName
0 John Marwel John-Marwel
1 Doe Williams Doe-Williams
2 Bill Harry Bill-Harry
Using apply()
You can use DataFrame.apply() for concatenate multiple column values into a single column, with slightly less typing and more scalable when you want to join multiple columns .
df['FullName'] = df[['First_Name', 'Last_Name']].apply(lambda x: '_'.join(x), axis=1)
df
First_Name Last_Name FullName
0 John Marwel John_Marwel
1 Doe Williams Doe_Williams
2 Bill Harry Bill_Harry
Related Topics
- Creating an empty Pandas DataFrame
- How to Check if a Pandas DataFrame is Empty
- How to check if a column exists in Pandas Dataframe
- How to delete column from pandas DataFrame
- How to select multiple columns from Pandas DataFrame
- Selecting multiple columns in a Pandas dataframe based on condition
- Selecting rows in pandas DataFrame based on conditions
- How to Drop rows in DataFrame by conditions on column values
- Rename column in Pandas DataFrame
- Get a List of all Column Names in Pandas DataFrame
- How to add new columns to Pandas dataframe?
- Change the order of columns in Pandas dataframe
- How to count the number of rows and columns in a Pandas DataFrame
- Use a list of values to select rows from a pandas dataframe
- How to iterate over rows in a DataFrame in Pandas
- How to drop rows/columns of Pandas DataFrame whose value is NaN
- How to Export Pandas DataFrame to a CSV File
- Convert list of dictionaries to a pandas DataFrame
- How to set a particular cell value in pandas DataFrame