-2

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?

2 Answers2

1

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

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 19 '22 at 11:49
0

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
fsimonjetz
  • 5,644
  • 3
  • 5
  • 21