Join Text of Two Columns in pandas DataFrame in Python (Example Code)

This article explains how to concatenate text in variables of a pandas DataFrame in Python.

Creation of Example Data

import pandas as pd                                # Import pandas
my_df = pd.DataFrame({'A':['xxx', 'yyy', 'zzz'],  # Construct DataFrame in Python
                      'B':['hey', 'hi', 'yoyo']})
print(my_df)                                      # Display DataFrame in console
#      A     B
# 0  xxx   hey
# 1  yyy    hi
# 2  zzz  yoyo

Example: Merge Multiple Text Columns of pandas DataFrame

my_df['C'] = my_df['A'] + my_df['B']              # Combine two variables
print(my_df)                                      # Display updated DataFrame
#      A     B        C
# 0  xxx   hey   xxxhey
# 1  yyy    hi    yyyhi
# 2  zzz  yoyo  zzzyoyo

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top