[Python] Reading negative numbers with minus-sign suffixed in Pandas

Discussão em 'Python' iniciado por Stack, Maio 19, 2025.

  1. Stack

    Stack Membro Participativo

    I have a dataframe column df_Fin['Rate'] of type object:

    1. 0.02
    2. 0.15
    3. 0.80-
    4. 0.10-
    5. 0.50


    where the hyphens indicate negative numbers.

    I tried to remove the hyphens and convert the column to float:

    df_Fin['Rate2'] = df_Fin['Rate'].replace('-','', regex = True)
    df_Fin['Rate2'] = df_Fin['Rate2'].astype(float)


    This removes the hyphen and lets me do math on it, but I need to know which rows had negative numbers.

    But when I do:

    df_Fin['Negative Rates'] = np.where(df_Fin['Rate'].str.contains('-'), -1, 1)


    I get all -1s in df_Fin['Negative Rates'].

    How can I read or convert the negative numbers, even if the minus sign is suffixed ?

    Continue reading...

Compartilhe esta Página