Home > other >  Pandas: Replace and remove character in columns
Pandas: Replace and remove character in columns

Time:12-07

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

CodePudding user response:

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 ]
  • Related