Strip whitespace from strings in a column

You can strip() an entire Series in Pandas using .str.strip():

df1['employee_id'] = df1['employee_id'].str.strip()
df2['employee_id'] = df2['employee_id'].str.strip()

This will remove leading/trailing whitespaces on the employee_id column in both df1 and df2

Alternatively, modify the read_csv lines to use skipinitialspace=True

df1 = pd.read_csv('input1.csv', sep=',\s+', delimiter=",", encoding="utf-8", skipinitialspace=True)
df2 = pd.read_csv('input2.csv', sep=',\s,', delimiter=",", encoding="utf-8", skipinitialspace=True)

It looks like you are attempting to remove spaces in a string containing numbers, which can be accomplished with pandas.Series.str.replace:

df1['employee_id'] = df1['employee_id'].str.replace(" ", "")
df2['employee_id'] = df2['employee_id'].str.replace(" ", "")

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)