Issue
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?
Solution
You can convert to string and extract the integer using regular expressions.
df['B'].str.extract('(\d+)').astype(int)
Answered By - Lokesh A. R.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.