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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *