python - pandas: Dataframe.replace() with regex -
i have table looks this:
df_raw = pd.dataframe(dict(a = pd.series(['1.00','-1']), b = pd.series(['1.0','-45.00','-']))) b 0 1.00 1.0 1 -1 -45.00 2 nan -
i replace '-' '0.00' using dataframe.replace() struggles because of negative values, '-1', '-45.00'.
how can ignore negative values , replace '-' '0.00' ?
my code:
df_raw = df_raw.replace(['-','\*'], ['0.00','0.00'], regex=true).astype(np.float64)
error code:
valueerror: invalid literal float(): 0.0045.00
your regex matching on -
characters:
in [48]: df_raw.replace(['-','\*'], ['0.00','0.00'], regex=true) out[48]: b 0 1.00 1.0 1 0.001 0.0045.00 2 nan 0.00
if put additional boundaries matches single character termination works expected:
in [47]: df_raw.replace(['^-$'], ['0.00'], regex=true) out[47]: b 0 1.00 1.0 1 -1 -45.00 2 nan 0.00
here ^
means start of string , $
means end of string match on single character.
or can use replace
match on exact matches:
in [29]: df_raw.replace('-',0) out[29]: b 0 1.00 1.0 1 -1 -45.00 2 nan 0
Comments
Post a Comment