Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I split last 3 digits in values in a column in a Dataframe into two new Dataframes?

Tags:

python

pandas

df=pd.DataFrame({'col1':[25021,25002,8002,40211,2232,""]})

    col1    
0   25021    
1   25002
2   8002
3   40211
4   2232
5   

I would like to get the following, not too sure how to split based on last 3 digits into col3 and whatever preceding into col1

    col2   col3    
0   25     021       
1   25     002       
2   8      002      
3   40     211       
4   2      232 
5
like image 330
bdoe85 Avatar asked Sep 06 '25 21:09

bdoe85


1 Answers

This is my approach:

df['col2'] = df['col1'].astype(str).str[-3:]

df['col1'] = df['col1'].astype(str).str[:-3]

Output:

   col1 col2
0    25  021
1    25  002
2     8  002
3    40  211
4     2  232
like image 141
Quang Hoang Avatar answered Sep 09 '25 11:09

Quang Hoang