Combine multiple column values into a single column

To combine multiple column values into a single column in Pandas, you can use the apply() method along with a custom function or the + operator to concatenate the values. Alternatively, you can use string formatting or other built-in string manipulation functions to achieve the desired result. By combining the values, you can create a new column with the merged information from the original columns.

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

When you need to join multiple string columns in a DataFrame, you can utilize the agg() method with a custom lambda function that performs the concatenation. This approach is especially useful when you want to concatenate specific columns and aggregate them based on a certain condition or separator. By using agg(), you can easily control how the concatenation is performed and apply it to multiple columns simultaneously.

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()

Using DataFrame.apply() is a powerful and flexible approach to concatenate multiple column values into a single column. It allows you to apply a custom function to each row or column of the DataFrame, making it easy to combine values from multiple columns into a new single column. This method is particularly useful when you have a large number of columns to concatenate or when you want to perform more complex operations during the concatenation process.

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

Conclusion

To combine multiple column values into a single column in a Pandas DataFrame, you can use methods like apply() or agg() with custom lambda functions. These approaches provide flexibility in how the concatenation is performed and allow you to join specific columns based on your requirements.