I have a df with much values like that BB1283980901 and i have to separate all the two first values witch will aways be two letters. How do i can do it, pls?
CodePudding user response:
You can perform string slicing on columns as follows:
import pandas as pd
df = pd.DataFrame({"s":["BB1283980901"]})
df['s1'] = df['s'].str[:2]
df['s2'] = df['s'].str[2:]
print(df)
Please check this post for more info
CodePudding user response:
str.extract()
is an option, where (..)
captures the first two characters, and (.*)
gets the rest:
>>> df = pd.DataFrame({"X": ["BB1283980901", "AA1283980901"]})
>>> df
X
0 BB1283980901
1 AA1283980901
>>> df[["prefix", "foobar"]] = df.X.str.extract("(..)(. )")
>>> df
X prefix foobar
0 BB1283980901 BB 1283980901
1 AA1283980901 AA 1283980901