0

I have this dataframe , and I want to replace any cell that contains an alphabetic character with an empty value .

df = pd.DataFrame(dict(A = pd.Series(['AB5 La2','-1','8577Y--00']), B = pd.Series(['2\nDate','-45.00','-'])))

df.replace(['.*[a-zA-Z].*'], [''], regex=True , inplace=True)

df

Initially , the dataframe was : enter image description here

i got this dataframe : enter image description here

it seems like it doesn't replace all the cell

All i want is to replace all cell with empty value when it contains an alphabetic character

I appreciate any help , thank you

Maryem Samet
  • 103
  • 3
  • 18
  • `.` does not match newlines by default. Use the `s` inline DOTALL modifier, `(?s).*[a-zA-Z].*`. BUT a much better regex is `(?s)[^a-zA-Z]*[a-zA-Z].*` – Wiktor Stribiżew Jun 16 '19 at 17:53

1 Answers1

1
\s
Matches any whitespace character; this is equivalent to the class [ \t\n\r\f\v].

https://docs.python.org/3/howto/regex.html

In [44]: df = pd.DataFrame(dict(A = pd.Series(['AB5 La2','-1','8577Y--00']), B = pd.Series(['2\nDate','-45.00','-']))) 
    ...:  
    ...: df.replace(['.*(\s)?[a-zA-Z].*'], [''], regex=True , inplace=True)                                                                                                                  

In [45]: df                                                                                                                                                                                   
Out[45]: 
    A       B
0            
1  -1  -45.00
2           -
frankegoesdown
  • 1,660
  • 1
  • 12
  • 29