String concatenation of two pandas columns

Learn, how to concatenate string of two pandas columns? By Pranit Sharma Last updated : October 06, 2023

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

String concatenation

The string is a group of characters, these characters may consist of all the lower case, upper case, and special characters present on the keyboard of a computer system. A string is a data type and the number of characters in a string is known as the length of the string.

To concatenate the strings, we will simply extract the required strings and use the + operator to concatenate the strings.

Let us understand with the help of an example,

Python program for string concatenation of two pandas columns

# Import pandas
import pandas as pd

# Import numpy
import numpy as np

# Creating a dataframe
df = pd.DataFrame({'A':['a','b','c','d'], 'B':['e','f','g','h']})

# Display original dataframe
print("Original DataFrame:\n",df,"\n")

# Concatenating strings
df['New'] = df.A.map(str) + " . " + df.B

# Display new df
print("Modified DataFrame:\n",df,"\n")

Output

The output of the above program is:

Example: String concatenation of two pandas columnsframe

Python Pandas Programs »


Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.