Combine two columns of text in pandas dataframe

In pandas, you can use the str.cat() function to combine the values of two columns of text into a single column. You can specify the columns to be combined and the separator to be used between them. Here is an example of how you can combine the values of two columns "column1" and "column2" into a new column "new_column" with a separator of "-" in a dataframe "df":

df['new_column'] = df['column1'].str.cat(df['column2'], sep='-')

This will create a new column named new_column in the dataframe which will have the combined value of column1 and column2 separated by "-"

Watch a course Python - The Practical Guide

You can also use the .apply() function and a lambda function to achieve the same thing. Here is an example:

df["new_column"] = df.apply(lambda x : x["column1"] + '-' + x["column2"], axis = 1)

Alternatively, you can also use the .join() method of the string representation of the dataframe, this method allow you to join two columns into a single column using a specified separator, the following example show how to join the two columns "column1" and "column2" using "-" separator

df["new_column"] = df["column1"] + "-" + df["column2"]

Choose the one that best fits your use case and data.