Lets say I have a dataframe df
as
A B 1 V2 3 W42 1 S03 2 T02 3 U71
I want to have a new column (either at it the end of df
or replace column B
with it, as it doesn't matter) that only extracts the int from the column B
. That is I want column C
to look like
C 2 42 3 2 71
So if there is a 0 in front of the number, such as for 03, then I want to return 3 not 03
How can I do this?
You can convert to string and extract the integer using regular expressions.
df['B'].str.extract('(\d+)').astype(int)
Assuming there is always exactly one leading letter
df['B'] = df['B'].str[1:].astype(int)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With