80

I am using python csvkit to compare 2 files like this:

df1 = pd.read_csv('input1.csv', sep=',\s+', delimiter=',', encoding="utf-8")
df2 = pd.read_csv('input2.csv', sep=',\s,', delimiter=',', encoding="utf-8")
df3 = pd.merge(df1,df2, on='employee_id', how='right')
df3.to_csv('output.csv', encoding='utf-8', index=False)

Currently I am running the file through a script before hand that strips spaces from the employee_id column.

An example of employee_ids:

37 78973 3
23787
2 22 3
123

Is there a way to get csvkit to do it and save me a step?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
fightstarr20
  • 11,682
  • 40
  • 154
  • 278

6 Answers6

136

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(" ", "")
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Andy
  • 49,085
  • 60
  • 166
  • 233
35

You can do the strip() in pandas.read_csv() as:

pandas.read_csv(..., converters={'employee_id': str.strip})

And if you need to only strip leading whitespace:

pandas.read_csv(..., converters={'employee_id': str.lstrip})

And to remove all spaces:

def strip_spaces(a_str_with_spaces):
    return a_str_with_spaces.replace(' ', '')

pandas.read_csv(..., converters={'employee_id': strip_spaces})
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
  • Would it be pythonic if I used my own converter that either returns the result of `str.strip` or `None`? I'm importing data from Excel and it would be great if I could turn empty cells into `None` without additional steps, but I'm not sure whether this kind of _magic_ is _legal_. – t3chb0t Sep 30 '22 at 18:38
  • dataframe cells can be None if desired. – Stephen Rauch Oct 01 '22 at 18:33
10
Df['employee']=Df['employee'].str.strip()
Andy
  • 49,085
  • 60
  • 166
  • 233
Vipin
  • 4,851
  • 3
  • 35
  • 65
8

The best and easiest way to remove blank whitespace in pandas dataframes is :-

df1 = pd.read_csv('input1.csv')

df1["employee_id"]  = df1["employee_id"].str.strip()

That's it

Saeed Khan
  • 109
  • 1
  • 2
2

In a dataframe (df) there may be multiple column name that have ' SPACE '. One of the general and easy way to do away with that is :- (df is the dataframe)

df.columns = df.columns.str.replace(' ', '')
2

also there is skipinitialspace

Skip spaces after delimiter.

from: https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html

Example:

df1 = pd.read_csv('input1.csv', skipinitialspace=True)
JoeSchr
  • 1,067
  • 1
  • 9
  • 14