(Modified) I want to separate letters and numbers from Python Data Frame and make them into new columns.

Asked 2 years ago, Updated 2 years ago, 55 views

Hello, I'm a beginner studying DataFrame at Python. I want to distinguish between letters and numbers in the specific column of the df. I looked it up and it seems like you're using the import re. I don't know what to do if there's a mix of numbers and letters.


df = pd.DataFrame({"idx4":["ex100f", "ew20", "ed200", None, None,"ed200f", "ed30f",None,"ew2",None,"ex3"] })
If there's a df like this, I want to get rid of the first "e" and separate the numbers from the letters to make idx5, idx6, idx7.

a1  idx3       idx4
b   ex1.x.1     ex100f
c   ex1.x       ew20
d   ex2.x.2     ed200
e   ex2.x.3 
f   ex1.x.2 
g   ex2.x       ed200f
h   ex2.x.4     ed30f
i   ex2.x.5 
j   ex1.x.3     ew2
k   ex3.x   
l   ex2.x.6     ex3
m   ex2.x.7     ed200f


 I want to make it as below in the above df!

a1  idx3     idx4   idx5    idx6    idx7
b   ex1.x.1  ex100f   x      100        f
c   ex1.x    ew20     w      20 
d   ex2.x.2  ed200    d      200    
e   ex2.x.3             
f   ex1.x.2             
g   ex2.x    ed200f   d      200        f
h   ex2.x.4  ed30f    d      30         f
i   ex2.x.5             
j   ex1.x.3  ew2      w      2  
k   ex3.x               
l   ex2.x.6  ex3      x      3  
m   ex2.x.7  ed200f   d      200        f

python dataframe

2022-09-20 15:09

1 Answers


import pandas as pd
import re

def getNumber(myData):
    myStringList = re.findall(r'\d+', str(myData))
    if len(myStringList) > 0:
        return int(myStringList[0])
    else:
        return ''


if __name__ == '__main__':
    df = pd.DataFrame(
        {
            "idx4": ["ex100f", "ew20", "ed200", None, None, "ed200f", "ed30f", None, "ew2", None, "ex3"]
        }
    )
    df['idx5'] = df['idx4'].apply(lambda x: x[1] if x is not None else '')
    df['idx6'] = df['idx4'].apply(lambda x: getNumber(x))
    df['idx7'] = df['idx4'].apply(lambda x: x[-1] if x is not None and x[-1].isalpha() else '')
    print(df)


2022-09-20 15:09

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.