Home > OS >  How to format string without leading zeros and - as integer in python pandas?
How to format string without leading zeros and - as integer in python pandas?

Time:09-23

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

CodePudding user response:

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)

CodePudding user response:

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

df['col'] = pd.to_numeric(df['col'])
  • Related