How to Concatenate Column Values in a Pandas DataFrame

Here are two common ways to concatenate column values in a Pandas DataFrame:

(1) For strings only:

df["new_column"] = df["first_column"] + df["second_column"]

(2) For a mix of strings and integers (or only integers):

df["new_column"] = df["first_column"].map(str) + df["second_column"].map(str)

When numeric columns are involved, you must convert them to strings before concatenation. Otherwise, Python will raise a type error.

Below are complete, independently runnable examples.

Example 1: Concatenating Values Within a Single DataFrame

Assume you have the following dataset:

daymonthyear
1Jan2020
2Feb2021
3Mar2022
4Apr2023
5May2024

The goal is to create a new column in this format:

day-month-year

Here is the complete Python script:

import pandas as pd

data = {
    "day": [1, 2, 3, 4, 5],
    "month": ["Jan", "Feb", "Mar", "Apr", "May"],
    "year": [2020, 2021, 2022, 2023, 2024],
}

df = pd.DataFrame(data)

df["full_date"] = (
    df["day"].map(str)
    + "-"
    + df["month"]
    + "-"
    + df["year"].map(str)
)

print(df)

Output:

   day month  year   full_date
0    1   Jan  2020  1-Jan-2020
1    2   Feb  2021  2-Feb-2021
2    3   Mar  2022  3-Mar-2022
3    4   Apr  2023  4-Apr-2023
4    5   May  2024  5-May-2024

This script is fully independent and will run as-is.

Example 2: Concatenating Columns from Two Separate DataFrames

Now, assume you have two DataFrames.

First DataFrame:

daymonthyear
1Jan2020
2Feb2021
3Mar2022
4Apr2023
5May2024

Second DataFrame:

inflation_rateinterest_rate
4.51.25
4.21.50
4.81.75
5.02.00
4.62.25

The goal is to generate values like:

day-month-year: Inflation; Interest

Here is the complete script:

import pandas as pd

data1 = {
    "day": [1, 2, 3, 4, 5],
    "month": ["Jan", "Feb", "Mar", "Apr", "May"],
    "year": [2020, 2021, 2022, 2023, 2024],
}

df1 = pd.DataFrame(data1)

data2 = {
    "inflation_rate": [4.5, 4.2, 4.8, 5.0, 4.6],
    "interest_rate": [1.25, 1.50, 1.75, 2.00, 2.25],
}

df2 = pd.DataFrame(data2)

combined_values = (
    df1["day"].map(str)
    + "-"
    + df1["month"]
    + "-"
    + df1["year"].map(str)
    + ": Inflation "
    + df2["inflation_rate"].map(str)
    + "; Interest "
    + df2["interest_rate"].map(str)
)

print(combined_values)

Output:

0    1-Jan-2020: Inflation 4.5; Interest 1.25
1    2-Feb-2021: Inflation 4.2; Interest 1.5
2    3-Mar-2022: Inflation 4.8; Interest 1.75
3    4-Apr-2023: Inflation 5.0; Interest 2.0
4    5-May-2024: Inflation 4.6; Interest 2.25
dtype: object

Example 3: Concatenate Numeric Columns and Find the Maximum

In this example, both DataFrames contain only numeric values. After concatenation, you will calculate the maximum value.

import pandas as pd

data1 = {"group1": [60, 24, 18, 85, 39]}
df1 = pd.DataFrame(data1)

data2 = {"group2": [31, 47, 29, 76, 52]}
df2 = pd.DataFrame(data2)

concatenated = df1["group1"].map(str) + df2["group2"].map(str)

combined_df = pd.DataFrame(concatenated, columns=["combined"])

max_value = combined_df["combined"].max()

print(max_value)

Output:

8576

Conclusion

You have seen how to concatenate column values in a Pandas DataFrame using complete, runnable scripts.

Key points:

  • Use direct addition for string columns.
  • Convert numeric columns to strings using map(str) before concatenation.
  • Always include the necessary imports and DataFrame creation so the script runs independently.

This approach is useful for formatting dates, creating labels, generating identifiers, and preparing structured text outputs from your data.

See also: 5 Practical Ways to Use IF Conditions in a Pandas DataFrame

Vinish Kapoor
Vinish Kapoor

An Oracle ACE and software veteran with 25+ years of experience, passionate about AI and IT innovation.

guest

0 Comments
Oldest
Newest Most Voted