You can use
.str.extract()
with regular expression pattern. Examples:Output:In [13]: df = pd.DataFrame({'C':['123456', '789012', '345678'], 'D':['12345678', '123', '2']})
In [14]: df
Out[14]:
C D
0 123456 12345678
1 789012 123
2 345678 2
In [15]: df.C.str.extract("(.{3})(.{3})") # works only for strings with length 6
Out[15]:
0 1
0 123 456
1 789 012
2 345 678
In [16]: df.D.str.extract("(?=(.{,3})).*?(.{,3}$)") # should work for any length of s, same as s[:3] and s[-3:]
Out[16]:
0 1
0 123 678
1 123 123
2 2 2