Categories
Python Answers

How to combine two columns of text in a Python Pandas dataframe?

Spread the love

To combine two columns of text in a Python Pandas dataframe, we get the values from the columns and combine them with operators.

For instance, we write

df["period"] = df["Year"] + df["quarter"]

to concatenate the 'Year' and 'quarter' values together to form the period column.

We can also convert columns to strings with astype by writing

df["period"] = df["Year"].astype(str) + df["quarter"]

before we do concatenation.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.