Convert decimal separator
<p>I'm loading a CSV where decimal value separator is <code>,</code> and I would like to replace it by <code>.</code> in order to proceed the analysis.</p>

<p>I see the <code>converters</code> option in <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html" rel="nofollow">pandas.read_csv</a> but to use it I need to provide a list of all column names (which I want to convert), which might not be a good idea since there are lots of columns.</p>

<p>What I have in mind is to look each cell in all columns and replace it. </p>

<pre><code>ii = len(list(df))-1
print ii
jj = len(df.ix[:,0])
print jj
for i in range(0, ii):
    for j in range(0, jj):
        df.ix[i,j] = df.ix[i,j].to_string().replace(',' , '.')
</code></pre>

<p>Is there a better approach?</p>

<p>You don't have to provide all the column names to <code>converter</code>. </p>

<p>Give only those columns you want to convert </p>

<p>It would be <code>converter = {'col_name':lambda x : str(x).replace(',','.')}</code></p>

<p><b>EDIT</b> after rewording question.</p>

<p>Is this the best way to do it?</p>

<p>I would say yes. OP mentioned that there are a large number of columns he/she wants to convert and feels that a <code>dict</code> go out of hand. IMO, it will not. There are two reasons to why it wouldn't. </p>

<p>The first reason is that eventhough you have a large number of columns, I assume there is some pattern to it, (like the column numbers 2, 4... need to be converted). You could run a <code>for</code> loop or a list comprehension to generate this <code>dict</code> and pass it to the converter. Another advantage is that converters accept both column label as well as column index as keys so you don't have to mention the column labels.</p>

<p>Second, a <code>dict</code> is implemented using a hash table. This ensures that worst case look-up is constant time. So you don't have to worry about slow runtimes when using a large number of elements in the dictionary.</p>

<p>Though your method is correct, IMO it is reinventing the wheel.</p>

python python-2.7 pandas dataframe