Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas keep numerical part

I have a set of data like:

        0       1 
0  type 1  type 2
1  type 3  type 4

How can I transfer it to:

   0  1
0  1  2
1  3  4

perfer using applyor transform function

like image 265
muuuuuj Avatar asked Apr 25 '26 06:04

muuuuuj


2 Answers

Yoou can use DataFrame.replace:

print (df.replace({'type ': ''}, regex=True))
   0  1
0  1  2
1  3  4
like image 77
jezrael Avatar answered Apr 27 '26 19:04

jezrael


>>> df.apply(lambda x: x.str.replace('type ','').astype(int))
   0  1
0  1  2
1  3  4

remove the .astype(int) if you don't need to convert to int

like image 20
Asish M. Avatar answered Apr 27 '26 19:04

Asish M.