Pandas: Replace and remove character in columns

Pandas: Replace and remove character in columns

Problem Description:

I have a dataframe in pandas, in this format:
I need to perform formatting on my dataframe that is larger than this, generally speaking only on the ‘CTe’ column

CTe = ["1221-2","12321-45","123-3"]
UF = ['A','B','C']

df = pd.DataFrame(
    data = zip(CTe,UF),
        columns=["CTe","UF"])

And I would like to know how I can format the entire "CTe" column, where I can remove the ‘-‘ and the numbers after the ‘-‘. The result I expect is the following:

CTe = ["1221","12321","123"]
UF = ['A','B','C']

df = pd.DataFrame(
    data = zip(CTe,UF),
        columns=["CTe","UF"])

I’m asking this because I just need to do a "merge" and my other dataframe only has the number that is before the ‘ – ‘.

I don’t know what I can do

Solution – 1

Something like this?

df['CTe'].str.split('-', 1).str[0]

Alternatively clean the CTe list before creating the dataframe:

CTe_cleaned = [''.join(x.split('-')[0]) for x in CTe ]
Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject