Question Details

No question body available.

Tags

python pandas

Answers (2)

February 20, 2026 Score: 2 Rep: 2,176 Quality: Low Completeness: 50%

The method str.removesuffix expects a string as its argument, not a series. When you pass a Series (df['Domain']), it does not work element-wise, resulting in empty or null values.

You can use a row-wise lambda to remove the domain suffix for each row.

df['Hostname'] = df.apply(
    lambda row: row['Hostname'][:-len(row['Domain'])] if row['Hostname'].endswith(row['Domain']) else row['Hostname'],
    axis=1
)

You might want to care about the additional '.' at the end of the hostname that would survive this replacement, if you have a typical combination such as 'server1.example.com' vs. 'example.com'. The above, however, does what you tried in your code.

February 20, 2026 Score: -2 Rep: 17 Quality: Low Completeness: 40%

Maybe you can try to iterate it like this:

df2 = df.copy()
for suffix in df2["Domain"]:
    df2["Hostname"] = df2["Hostname"].str.removesuffix(suffix)

print(df2)