How to format string without leading zeros and + – as integer in python pandas?

Question:

I have rows in a column with strings in a pandas dataframe that look like this:

+0094
-0082

How would I replace all the values in the column so that they would be formatted like this:

 94
-82
Asked By: Josh Johnson

||

Answers:

pandas is smart enough to figure it out with a dataframe’s astype method.

So your approach would be:

df['col of interest'] = df['col of interest'].astype(int)
Answered By: Maurice Pasternak

I’d suggest to use pd.to_numeric as it provides more flexibility over .astype :

df['col'] = pd.to_numeric(df['col'])
Answered By: ThePyGuy
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.