Reputation: 43
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
Upvotes: 4
Views: 1081
Reputation: 28709
Just a play on Pandas' string split method; you can wrap the delimiter(in a regex), so that it is included in the output:
(df
.astype(str)
.col1
.str.split(r'(\d{3}$)', n=1, expand=True)
.drop(2,axis=1)
.set_axis(['col1','col2'],axis='columns')
)
col1 col2
0 25 021
1 25 002
2 8 002
3 40 211
4 2 232
Upvotes: 0
Reputation: 150785
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
Upvotes: 3
Reputation: 2624
Try this.
df=pd.DataFrame({'col1':[25021,25002,8002,40211,2232]})
df['col2'] = df['col1'].astype(str).apply(lambda x:x[-3:]).astype(int)
df['col1'] = df['col1'].astype(str).apply(lambda x:x[:-3]).astype(int)
Upvotes: 0