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
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)
I’d suggest to use pd.to_numeric
as it provides more flexibility over .astype
:
df['col'] = pd.to_numeric(df['col'])
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
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)
I’d suggest to use pd.to_numeric
as it provides more flexibility over .astype
:
df['col'] = pd.to_numeric(df['col'])