Pandas - how to remove spaces in each column in a dataframe?
<p>I'm trying to remove spaces, apostrophes, and double quote in each column data using this for loop</p>

<p><code>for c in data.columns:
    data[c] = data[c].str.strip().replace(',', '').replace('\'', '').replace('\"', '').strip()</code></p>

<p>but I keep getting this error:</p>

<p><code>AttributeError: 'Series' object has no attribute 'strip'</code></p>

<p>data is the data frame and was obtained from an excel file</p>

<p><code>xl = pd.ExcelFile('test.xlsx');
data = xl.parse(sheetname='Sheet1')</code></p>

<p>Am I missing something?  I added the <code>str</code> but that didn't help.  Is there a better way to do this.</p>

<p>I don't want to use the column labels, like so <code>data['column label']</code>, because the text can be different.  I would like to iterate each column and remove the characters mentioned above.</p>

<p>incoming data:</p>

<p><code>id        city      country
1       Ontario       Canada
2     Calgary '        Canada'
3   'Vancouver        Canada</code></p>

<p>desired output:</p>

<p><code>id  city      country
1   Ontario     Canada
2   Calgary     Canada
3   Vancouver   Canada</code></p>

<p>data[c] does not return a value, it returns a series (a whole column of data).  </p>

<p>You can apply the strip operation to an entire column df.apply.  You can apply the strip function this way.</p>

python pandas