Issues with try/except, attempting to convert strings to integers in pandas data frame where possible
<p>I made a function to clean up any HTML code/tags from strings in my dataframe. The function takes every value from the data frame, cleans it with the remove_html function, and returns a clean df. After converting the data frame to string values and cleaning it up I'm attempting to convert where possible the values in the data frame back to integers. I have tried try/except but don't get the result that I want. This is what I have at the moment:</p>

<pre><code>def clean_df(df):
    df = df.astype(str)
    list_of_columns = list(df.columns)
    for col in list_of_columns:
        column = []
        for row in list(df[col]):
            column.append(remove_html(row))
            try:
                return int(row)
            except ValueError:
                pass

        del df[col]

        df[col] = column

    return df
</code></pre>

<p>Without the try/except statements the function returns a clean df where the integers are strings. So its just the try/except statement that seems to be an issue. I've tried the try/except statements in multiple ways and none of them return a df. The current code for example returns an 'int' object.</p>

<p>consider the <code>pd.DataFrame</code> <code>df</code></p>

<pre><code>df = pd.DataFrame(dict(A=[1, '2', '_', '4']))
</code></pre>

<p><a href="https://i.stack.imgur.com/m05NY.png" rel="nofollow"><img src="https://i.stack.imgur.com/m05NY.png" alt="enter image description here"></a></p>

<p>You want to use the function <code>pd.to_numeric</code>...<br>
<strong><em>Note</em></strong><br>
<code>pd.to_numeric</code> operates on scalars and <code>pd.Series</code>.  It doesn't operate on a <code>pd.DataFrame</code><br>
<strong><em>Also</em></strong><br>
Use the parameter <code>errors='coerce'</code> to get numbers where you can and <code>NaN</code> elsewhere.</p>

<pre><code>pd.to_numeric(df['A'], 'coerce')

0    1.0
1    2.0
2    NaN
3    4.0
Name: A, dtype: float6
</code></pre>

<p>Or, to get numbers where you can, and what you already had elsewhere</p>

<pre><code>pd.to_numeric(df['A'], 'coerce').combine_first(df['A'])

0    1
1    2
2    _
3    4
Name: A, dtype: object
</code></pre>

<p>you can then assign it back to your <code>df</code></p>

<pre><code>df['A'] = pd.to_numeric(df['A'], 'coerce').combine_first(df['A'])
</code></pre>

python pandas try-except