Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract int from string in Pandas

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?

like image 728
user5739619 Avatar asked Feb 13 '16 05:02

user5739619


2 Answers

You can convert to string and extract the integer using regular expressions.

df['B'].str.extract('(\d+)').astype(int) 
like image 164
Lokesh A. R. Avatar answered Sep 28 '22 00:09

Lokesh A. R.


Assuming there is always exactly one leading letter

df['B'] = df['B'].str[1:].astype(int) 
like image 36
Mike Graham Avatar answered Sep 28 '22 00:09

Mike Graham