Trying to loop over colums in DataFrame and strip Dollar Sign
<p>My DataFrame has way too many columns to manully type in all of the columns separately. Therefore I am trying to loop through them quickly and get rid of the dollar signs and the commas in the large numbers. This is the code that I have so far: </p>

<pre><code>for column in df1:
    df1[column] = df1[column].str.lstrip('$')
</code></pre>

<p>and I am getting the error: </p>

<p>AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandas</p>

<p>You can filter just the str columns using <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.select_dtypes.html" rel="nofollow"><code>select_dtypes</code></a>:</p>

<pre><code>for col in df.select_dtypes([np.object]):
    df[col] = df[col].str.lstrip('$')
</code></pre>

<p>Example:</p>

<pre><code>In [309]:
df = pd.DataFrame({'int':np.arange(3), 'float':[0.1,2.3,4.0], 'str':['$d', 'a$', 'asd']})
df

Out[309]:
   float  int  str
0    0.1    0   $d
1    2.3    1   a$
2    4.0    2  asd

In [310]:
for col in df.select_dtypes([np.object]):
    df[col] = df[col].str.lstrip('$')
df

Out[310]:
   float  int  str
0    0.1    0    d
1    2.3    1   a$
2    4.0    2  asd
</code></pre>

python pandas dataframe