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?
            Asked
            
        
        
            Active
            
        
            Viewed 38 times
        
    -2
            
            
        - 
                    Guys, i forgot. My df have much columns and one unique column with values like that from question. And i have to create a new column – Robert Cleyton Rodrigues Mar 19 '22 at 11:35
2 Answers
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
- 
                    
- 
                    Man, what if i have to extract the number "398"? Or another number, can u help me? – Robert Cleyton Rodrigues Mar 20 '22 at 12:14
- 
                    It depends. Do you need *any* consecutive 3 digits? Or the slice `[5:7]` (in which case the answer by Dhiraj Eadara would be useful)? – fsimonjetz Mar 20 '22 at 20:53
