Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex: change "white space" chracter and - character to null

import pandas as pd
import numpy as np

df = pd.DataFrame([
    [-0.532681, 'foo sai', 0],
    [1.490752, 'bar', 1],
    [-1.387326, 'foo-', '-'],
    [0.814772, 'baz', ' - '],     
    [-0.222552, ' -', '   -'],
    [-1.176781,  'qux', '- '],         
], columns='A B C'.split())

print(df)
print('-------------------------------')

print(df.replace(r'[^\w][\s]', np.nan, regex=True))

how i replace any whitespace character and - with regex?
with my code, return this:

          A        B    C
0 -0.532681  foo sai    0
1  1.490752      bar    1
2 -1.387326     foo-    -
3  0.814772      baz  NaN
4 -0.222552        -  NaN
5 -1.176781      qux  NaN

but return that i expect is this:<br>
              A        B    C
    0 -0.532681  foo sai    0
    1  1.490752      bar    1
    2 -1.387326     foo-  Nan
    3  0.814772      baz  NaN
    4 -0.222552      Nan  NaN
    5 -1.176781      qux  NaN
like image 581
mgssalim Avatar asked Oct 15 '22 00:10

mgssalim


1 Answers

You may use

df.replace(r'^[\s-]+$', np.nan, regex=True)

Output:

          A        B    C
0 -0.532681  foo sai  0.0
1  1.490752      bar  1.0
2 -1.387326     foo-  NaN
3  0.814772      baz  NaN
4 -0.222552      NaN  NaN
5 -1.176781      qux  NaN

The ^[\s-]+$ pattern matches

  • ^ - start of string
  • [\s-]+ - one or more whitespace or - chars
  • $ - end of string.
like image 90
Wiktor Stribiżew Avatar answered Oct 18 '22 15:10

Wiktor Stribiżew