sign "-" after the number

Viewed 70

I have a dataframe containing a column with numbers like this:

 1. 34.5
 2. 345.2
 3. 898.4-
 4. 87.4

...

 n. 847.3-

The column is str, now I have written a code that cycles the whole column (about 19 million rows). I ask if anyone knows a faster method to bring the "-" in front and change the str to number

My code:

for i, row in frame.iterrows():
    Val=row[9]
    if row[9].find("-") > 0 :
        Val="-"+row[9].replace("-","")
        frame.iloc[i,9]=Val
frame[frame.columns[9]]=pd.to_numeric(frame[frame.columns[9]]
3 Answers

You can do it directly with the help of str:

frame.loc[frame[col_name].str[-1] == '-', col_name] = '-' + frame.loc[
                    frame[col_name].str[-1] == '-', col_name].str.replace('-', '')
frame[col_name] = frame[col_name].astype('float')

Let's try two calls to pd.to_numeric. The first gets all of the positive numbers correct, the second will then fill in the negative numbers that were coerced to NaN on the first try. Non-numeric values are still coerced to NaN.

import pandas as pd
s = pd.Series(['34.5', '345.2', '898.4-', '87.4', '847.3-', 'foobar', 'f-']) 

m = s.str[-1].eq('-')  # True for negative numbers

(pd.to_numeric(s, errors='coerce')
   .fillna(-1*pd.to_numeric(s.loc[m].str[:-1], errors='coerce'))
)

0     34.5
1    345.2
2   -898.4
3     87.4
4   -847.3
5      NaN
6      NaN
dtype: float64
Related